GIT, an awesome tool which made the life of programmer a heaven. This git tutorial focuses on step by step examples.
When I was a kid, I always wondered to go back to the past to check what actually happened and how?
Anyone, who is a fan of sci-fi movies would always think about the time machine. Well, this thing is still a dream but somehow git tool make it possible. Not in our real world but in the world of programming.
What is GIT?
Git is a version controlling tool which keeps track of changes in the code of software project.
Changes occur in the modules of the software project whenever the clients or users demand some new requirements. This tool helps us to keep track of changes.
The person behind Git is Linus Torvalds. Yes, the same person who developed the Linux kernel.
If you still haven’t got the idea, let’s make it simple. Think about a text file in notepad/Gedit.
Whenever you save the file after a particular change, the last time you saved something is the final file you have.
UNDO will also work till you exit the Notepad/Gedit editor with limitations.
Now in this scenario, Git is the beast. It will store copy whenever you commit changes in the file.
Here, commit in git means save in notepad editor with some enhanced functionality.
You can go through every commit and can see the changes till that point.
If still, you are having difficulty in understanding the concept just keep going through this post and see every example.
Following are the git commands which we are going to study. In the examples, you will be demonstrated with some HTML files to track the changes. In the real scenario, you can have multiple files or a whole project.
Let’s get started.
This command creates a new empty git repository in the current directory you are working in.
In the following screenshot, you could see a directory named /coderfolk which is having a file hello.html. I am going to use this file for the demonstration.
As shown in the screenshot, git init command created an empty repository. This commands created hidden directory .git, this directory contains all the configuration files which are need by a git repository. Use it in following way.
This command is use to add the files in the staged area. Staging area is the area where we add files before we commit the changes.
To add single file, following syntax you will need to follow.
git add <filename>
Use following command to add all the files.
git add .
To add files in the staging area make sure you create some files. In examples we are using hello.html file.
The git status command tells you the list of file in which you did some modifications.
Following screenshot shows the output of the command.
Use it in following way
This commands also gives information about how many commits you are ahead of the remote repository. Here remote branch means the branch on the server.
This command makes your changes permanent. With this
git commit -m <your_message>
Following is the output of this command.
After above the commit, I again modified the file. To demonstrate the example with some more commits.
Following screenshot shows the series of actions which were performed.
In simple words, using this command you upload all the changes/commits to the remote repository which is located on the server.
To create a remote repository on the above-mentioned websites, please readthis post.
To push the commits to remote use following command.
git push -u origin master
Above is the output given by command after entering username and password. You can check your remote repository, all of your commits will be there.
The git log commands list out all the information related to commits. This will give you information related to time and date of commit, unique id of commit, author name and the message associated with the commit.
Following screenshot gives details about git log
This command gives you information about the changes in the file. It will show the lines which are modified due to insertion and removal before you do your commit.
Check it out in following screenshot.
The git show is same as the git diff. It is used after the commit is done. It gives us information about the modifications and the commit itself as shown below.
This command creates the branches in the repository. Branches are the separate development flow in the project.
Branches helps the developers to collaborate on the project. Suppose two developers are working on the two tasks. They can work on two different branches.
After completion of the tasks, two branches can be merged with the command git merge.
The git branch command list down the branches in the local repository. Also, it shows the current branch we are in. Use it as follows.
To list down all the branches on the remote repository , use following command.
git branch -a
To create new branch, use following command
git branch <branch name>
Following screenshot shows the example all the above mentioned commands.
After creation of branch, we need to push the branch to the remote repository. To do that use, following command.
git push -u origin <branch_name>
Following image shows the example.
Now our new branch will be on remote repository.
This command is used to switch between one branch to another branch. To switch to another branch , use following command.
git checkout <branch_name>
Git checkout also helps us to create the new branch from the current branch. It can be done, as follows.
git checkout -b <new_branch_name>
Following image shows the example of git checkout.
I hope you understood the basic concepts behind this. Keep coding.