jump to navigation

Git Tutorial : Starting with git using just 10 commands August 13, 2008

Posted by xk0der in : Programming , trackback

Part – II of this tutorial is now available here.

Git is marvellous piece of tool every programmer must have. Git is a distributed version control system developed by Linus and others. Yes you guessed it, it is used heavily by the Linux (kernel) developers apart from many other projects.

How will Git help you and what this post covers.

Scope of this post : This post will deal with the usage of Git by an individual developer only.

Why use version control : Version control gives you a great deal of flexibility while coding. You can make changes without having to worry about breaking things, as you can always move back to previous version(s) of your code. Version control also allows you to compare changes as you move ahead, helping in bug-fixing apart from other benefits.

Why use Git : There are a number of other version control systems available, some centralized systems some distributed systems. Amongst them, Git is one of the fastest version control system, very simple and easy to use, and has lot of helpful features packed in very a small package. Check my other post to know more reasons.


1) You need a computer :) with preferably Linux (any flavor) installed on it. (This post does not deal with Windows, though you may use the commands given here without any modifications (probably!)).

2) You need Git.

on Fedora distribution, you may install git using the following command

$ sudo yum install git

On Ubuntu distribution, you may install git using the following command

$ sudo apt-get install git
$ sudo apt-get install git-core

(Edit: 21/Oct/2008 – Thanks to robo47 for pointing out the mistake)

For other methods and building from source check out this page.

3) You need some project which you are developing (or would be developing) :) . The project’s language could be anything .. C, C++, D, Python, Perl, HTML, JavaScript, plain text files … you got the idea right? :)

Let the show begin

Command 1 : Initializing an empty git repository.

Befor you can start using the power of git you need to create an empty repository and tell git which files to track. The first command that I’m presenting initializes an empty git repository.

Before executing the command you need to be at base folder of you project. For example if you have all the source code for you project stored here /home/xk0der/SourceCode/myProject, (We’ll call this folder as the base folder for ease of discussion), move there first. Now execute the following command once you are in the base folder.

$ git init

This will create an empty repository in the current directory (the base folder).

For every project that you want to track using git, go to its base folder and issue this command first and then the next 2 commands presented below.

Command 2 : Adding files to git repository.

The next step is to add files to your git repository, essentially telling git that these are the files we want to keep track of. Execute the following command from the base folder.

$ git add .

This will add all the files from the current folder and sub-folders recursively to the git repository.

Command 3 : The commit command.

This is one command you’ll be using quite often.

the above command adds the files but does not commit the changes to the git repository. So you need to issue the following command to make the changes final.

$ git commit -a

This will open a text editor (probably vi) where you can add some comments about this commit. If you prefer not to use the text editor, you can specify the commit message from the command line it self, just use the following format instead of the above one.

$ git commit -a -m "Initial import"

The -m switch is used to specify the commit message. In our case the commit message is “Initial import”. You can put anything between the quotes for your message.

You are ‘all’ ready!

Now open your favorite text editor and start coding. When you feel you have accomplished something or you just want to save the current state of your source code issue the git-commit command as described above. The commit command will create a new checkpoint and save the current status of your source code.

Git (like any other version control system) saves all the commits you have done to your source code repository. This enables you to go back and forth between commits and inspect the changes.

Now let’s move forward and learn a few more commands.

Command 4 : Viewing the commit log.

You can view all the commits you have done till now using the following command

$ git log

This command shows the commit history with the

1) Unique commit hash
2) Name and email of the person who performed the commit
3) Date and time of commit
4) The commit message

Of all these, the first item, the unique commit identifier, the SHA1 hash is of importance to us. This hash will be used with some of the commands listed next, and is used with a plethora of other git commands, not listed here :)

Command 5 : Checking the current status.

While coding, you may want to see what all files have changed, before you do a commit to store them in the git repository. This command helps you achieve that.

$ git status

The above command will list all files that have changed since your last commit.

Command 6 : Finding the difference between commits.

Apart from just viewing files that have changed, you may be interested in viewing the actual differences in the source code.

$ git diff

The above command will show a difference ( a diff ) with respect to your last commit and current changes.

The diff command can actually be used to compare difference between any commits

To view difference between a previous commit and current changes.

$ git diff commit_hash

here the commit_hash is the first eight characters (hex digits) of commit hash as shown in the commit log by the git-log command. Yes! you need not specify the full hash, just the first eight digits are good enough.

To view differences between two previous commits.

$ git diff commit_hash_1 commit_hash_2

This command will display differences between the two commits identified by the commit_hash_1 and commit_hash_2.

Command 7 : Creating branches!

Don’t be scared by the word “branching”, specially if you never dealt with them. Branches are easy to play with and are very useful.

Before I show you the command a bit more discussion about branches.

  • You can view branches as being diversion from the main linear commit tree.
  • All branches stem out from the master branch, the name given to the main trunk.
  • Every branch has it’s own commit log.
  • You can have different code in same files across difference branches. (This is the main feature of branching). Actually git goes even further and allows you to have not only different content in files across branches but even different sets of files across different branches.
  • At one point of time, you can be sitting only on one branch. What this means is that, the source code from the current branch will be the effective one, the one to which you would be making changes.

Why use branches?

Suppose you have an almost stable source code and you are to release the product in a month or in a week or two. You also have plans to include another great feature into your code, but are afraid it might break your code and your release plans may go hay-wire.

This is where branches come into picture. Just create an experimental branch from you main stable trunk ( the one you’ve been working on up till now is the main trunk, the master branch). After you have created the experimental branch, you can continue fixing bugs and polishing the code on the main, i.e. the master branch. Meanwhile you can also continue working on the experimental branch without affecting a line of code on the master branch.

In the end, if you find that the experimental feature is working good enough, merge it into the master branch and release your product. If the experimental feature didn’t quite work out, you can still release the product from the master branch. The experimental branch is still there so you can continue working on it till it gets stable.

This is one scenario (and the most often reason, though not the only one), to create branches.

Okay now, back to commands . Issue the following command when you want to create a branch.

Creating a branch

$ git branch branch_name

branch_name could be anything that you wish, for example:

$ git branch experimental_feature

Branching a branch or “I want more branches”.

You can create as many branches from inside any branch you want, creating a very dense tree if you like.

Just move inside (check-out that branch, see command 8 below) and issue the branch creation command.

Command 8 : Moving to a branch and listing all branches

Once you have created the branch move to it by issuing the command listed below. Always Make sure you do a commit before you issue this command or else changes will move across branches. Make this a habit! (OR have a look at git-stash command, discussed in part II of this tutorial.)

$ git checkout branch_name

Here the branch name is the branch where you want to move. Once checked-out, you can view staus, log, diff etc, using the commands presented earlier.

To go back to the master branch (the main trunk) issue the following command. (Again make sure you had issued the commit command)

$ git checkout master

Listing all branches

Issue the following command to see all available branches in the current repository.

$ git branch

Command 9 : Merging two branches

Move to (checkout) the branch with which you want the merge to happen and issue the following command.

$ git merge branch_name

This will merge the branch branch_name with the current branch.

For example if you want to merger the “experimental_feature” branch with the master branch, issue the following commands

$ git checkout master
$ git merge experimental_feature

Git will notify you with any conflicts it cannot resolve automatically (if any). You can then resolve the conflicts manually.

Deleting a branch

After the merge is done you can delete the experimental branch if you wish by issuing the command

$ git branch -d experimental_branch

The above command will only delete the branch if it is fully merged with the current branch’s HEAD. HEAD is the current position in your branch, the latest commit.

Command 10 : Deleting stuff from the repository.

Issue the following command if you do not want git to keep track of a file or folder and (this is the opposite of the git-add command). The file stays in the working directory, on the disk.

$ git rm --cached path/to/the/folder_or_file

The git-rm command removes the files from the repository for current HEAD only; previous revisions/commits will still have the file.

That’s it, you’re done!

Before I leave you :) a few more command/features that may interest you, but are not totally necessary as of now.

A graphical repository viewer

To invoke a graphical repository viewer, invoke the following command

$ gitk

Git – Graphical user interface

For those who prefer GUI, you can install a graphical front end to git.

On Fedora

$ yum install git-gui

On Ubuntu

$ apt-get install git-gui

Run the gui by issuing the following command

$ git-gui

Yup, totally done now! :)

That’s all folks. :) I hope this post will help you in getting started with one of the most powerful version control system available around.

When you are done playing with these commands, you may want to read the second installement of this turotial here: Git Tutorial Part II – Sharpen you Git-Fu with 10 more commands.

Like this post?


1. My first experience with git « Walk the walk - June 12, 2009

[…] Everyday git with 20 commands or so Small git tutorial […]

2. cho - August 11, 2009

great! easy to understand… 😀

3. linjava - August 12, 2009

Nice tut!

4. Pedro Sanders - August 19, 2009

Thanks xk0der,

As I can see is more easy and powerfull than others versioning systems.

(sorry for my english)

5. Kuroki Kaze - August 27, 2009

Whoa, very helpful, thanks 🙂 Git seems like very valuable tool, will look into it 🙂

6. Matteo - September 17, 2009

thanks easy nice end clean post

7. Learn GIT « Having fun with Web Development - October 28, 2009

[…] 1. Git Tutorial : Starting with git using just 10 commands […]

8. DJ Strouse - January 3, 2010

Best tutorial I found for total noobs. Thanks!

9. Github and the Mootools Forge: My experience. Samuel Thurston - January 5, 2010

[…] The Forge is tightly integrated with the “social coding” site Github. But the integration isn’t optional. This forced me to quickly familiarize myself with git, which was something I had been meaning to do anyway and just needed this type of impetus to do it.  Fortunately git is extremely intuitive for an old-school command line dweller such as myself, and Github’s own help site is a great resource.  I also found this blog post extremely useful as a git quick-reference. […]

10. Version control: Git tutorials and how-tos | FZ Blogs - February 12, 2010

[…] Git Tutorial : Starting with git using just 10 commands […]

11. Drew - March 7, 2010

Thank you much for the very straightforward tutorial. Easiest one I’ve found on google.

12. greg - March 19, 2010


13. allen - June 13, 2010

excellent work xkoder. tks!

14. karatedog - June 16, 2010

Git is very good, however it is much easier to shoot yourself in the foot with it, than, say with bazaar. Git is powerful, but it was developed for versioning the Linux kernel, and those kernel-fu guys won’t be scared by a suboptimal UI or special behavior.
For example take ‘git rm’ which should be instead what ‘git rm –cached’ does.
‘git rm’ will only delete the file from the current head, however it won’t check for it being in previous versions. So if you just created a file, but never made a commit, and do a ‘git-rm’ on it, that file is a goner.

15. Chickenfoot - March 30, 2011

You’d be surprised how many terrible tutorials there are on Google for intros to using Git.. yours was straightforward and easy to understand! Thank you 🙂

16. Sanjaykumar2325 - July 6, 2011

pls try to write understandable language with examples

17. Faern - October 25, 2012

 English doesn’t work?

18. Startup Ninja - July 8, 2011

great tutorial. 

19. YodleMonkey - January 12, 2012

Most git tutorials make my brain hurt. This one was clear, simple and useful.


20. 2 vídeo e 17 tutoriais sobre Github para você virar Ninja e sair detonando! | Desenvolvedor, Webdesigner e Pesquisador | deivison.com.br - February 1, 2012

[…] Git Tutorial : Starting with git using just 10 commands […]

21. neeraj shukla - March 10, 2013

Great one 🙂 Really.

22. Martin Hansen - April 10, 2013

Great tutorial but I still don’t get it. Why is there no working copy created? I can’t edit files in my repository directly, I need to use a local working directory and then commit when server is online. How to do that?

23. Martin Hansen - April 10, 2013

Never mind, I understood it. A working directory is also a repository in git. “git push” commits changes to a remote repository.

24. Confluence: Picard - April 17, 2013

Git Primer…

A basic primer for Git which was originally built…

25. Mohit Garg - November 1, 2013

awesum tutorial..very easy to understand

26. Technical: Installing and Using GIT Command Line Interface on CentOS | Daniel Adeniji's – Learning in the Open - April 26, 2014

[…] Git Tutorial starting with git using 10 commands http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/ […]

27. Arturo - September 29, 2014

awesome! I understood everything. Thanks a lot! Greeting from Venezuela!