How I use git

After many years of using SVN, my current revision control system of choice is git. It makes distributed and offline work a breeze, so I don’t always have to log in in to a remote repository when I want to commit some changes. This post describes how I use git on a regular basis.

For every project under revision control there is a local git repository

$ git init project

as well as a corresponding remote repository on my server which I use to synchronize different computers and as an extra backup facility.

$ git init --bare project.git

For sending committed changes to the remove repository as well as for retrieving them, I have a simple Makefile in place in the root directory of the project repository.

.PHONY: push pull
        git push --progress ssh://foo/project.git master:master
        git pull ssh://foo/project.git master:master

where foo is the actual address of the server plus the absolute path to the directory where all the git repositories reside.

I use .gitignore files extensively. For example, in a directory where I store LaTeX files, the .gitignore file would contain a list like this:


If I want to commit some changes I made, I first have to stage them as it is called in git terminology. This is done using the command

$ git add file1 file2 file3

This might seem a little odd or tedious at first, but staging lets you easily group changes to indicate that they are related.
To unstage a file use the command

$ git reset file

Committing then works the same as with SVN

$ git commit

One common mistake I make is forgetting to include some changes in a commit or staging changes that are not directly related. In that case there’s an easy solution to the problem. First you undo the last commit by issuing the command

$ git reset --soft HEAD^

Be sure to include the --soft option, because otherwise you’re committed changes will be overwritten by whatever revision that came before!
Then you make the changes, staging and unstaging or even editing a files (just be sure to stage edited files even if they are already in the so-called index, the list of files staged for the next commit) and commit with the command

$ git commit --amend

The followup post to this one will describe how I use git with Emacs.

This entry was posted in Tools and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.