Using Git Efficiently

I started using Git a few years ago, and to be sure, I cannot say that I know everything about it. However, I know enough of it to say that I am much more efficient using Git than other source control method.

In this post, I will summarize some of the commands everyone need to know in order to start using Git.


Creating a Repository

Creating a repository is just a matter of initialize a directory with git metadata, and add the files to the repository. For example, suppose you want to create a repository of directory /home/john/myproject To do this, you need to use the commands

cd /home/john/myproject
git init
git add .
git commit -m 'first version'

The first git command just initializes the .git directory, where all git metadata is stored. The second command adds everything under the current directory to the repository. Finally, the git commit command creates the initial revision of the project into the repository.


Making Changes

The next common operation in a repository is to make changes to the existing files. After you make the needed changes using your text editor or IDE, the following commands will do the trick:

git add ChangedFile1 ChangedFile2
git commit -m 'description of change'

The git add is necessary even if the file exists, because without it git doesn’t know if the files needs to be updated or not. In git parlance, git add is used to create the “staging area”, which will be written by the commit.

This is a difference from SVN, for example, where anytime we do a commit the system accepts all changes by default. With git you need to be specific about what is being commited.

Optionally, if you prefer to use a GUI to view the changes, you can use the command

git gui

This will invoke the Tk interface, which works both on UNIX and windows (and probably Mac OS X).


Deleting and Recovering

Another common operation is to remove files. This can be done with

git rm FileName

notice that this will work if the files hasn’t been modified. If you need to delete a file that has been changed, use

git rm -f FileName

To recover a file that has been deleted or changed, you can use the following

git checkout -- FileName

Creating Branches

With the commands above you can do pretty much anything you need in terms of normal usage of a VCS. However, creating branches is where the fun is. Git favors the creation of branches, because creating them is very fast and cheap (as measured in memory usage).

To create and use a new branch, type

git branch BranchName
git checkout BranchName

The first command creates the branch, while the second changes to the new branch. You can always go back using

git checkout master

where master is the default branch name.


Merging Files

Git makes it very easy to create branches, but also to merge the results of two or more branches. To merge a branch called “BranchName”, you can use the command

git merge BranchName

Git does a great work of solving conflicts automatically. However, if it can’t solve the conflict, you will be able to edit the files manually and commit them as normal.

You can visualise the results of merges and of other branches using a graphical tool. Just type

gitk --all

and you will be able to see all branches in the repository, along with the history. For a short log of changes, you can also type

git log

Advanced Tricks

There are thousands of advanced tricks you can learn with git, since each part of the system may work independently of the others. I will give you just a flavor of what you can do.

For example, if you want to change the order in which commit appear in a branch, you can use

git rebase -i CommitID

The commit id is the hexadecimal number listed on each commit when you use git log. The command above will call an editor and allow you to edit the commits that have been made since CommitID. You can change the order in which they are applied, merge two or more commits, or even remove some of them. In this way, you can rewrite the history of your commits, and make changes that you wouldn’t be able to do in something as SVN.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

One Response to “Using Git Efficiently”

  1. Advanced tricks is interactive rebase and bisect with run option.. fun!

    By Adam D. on Nov 7, 2009

Post a Comment