Tips and Tricks

If you have already learned the basics and feel comfortable with git, here is some tips and tricks you may want to employ to improve your workflow. Feel free to subscribe to the page to learn about new tricks as people add; or contribute your own.

git-config goodness

The "git config" command enables all kinds of interesting features that enhance one's experience with git. In general, when you set something with "git config --global" it will be saved in ~/.gitconfig and will apply to all your cloned repositories, whereas without --global it will only apply to the repository of your current directory.

Running "git config -l" lists all the current configurations.

Shortcut URLs

Always thought checking out a repository is easier done using a script than typing on the command line? No more. Just setup a URL scheme for your site of choice:

git config --global url.ssh://[login@] gnome:

Now you can simply do:

git clone gnome:pango

to checkout the pango tree. Yay!

Alias commands

You can define new git commands using the alias configuration. For example, to define "git up" to perform the "svn up" functionality, do:

git config --global alias.up "pull --rebase"

Or for "git ci" to work like "svn ci", do:

git config --global "commit -a"

You get the idea.

Rebasing by default

Many users find that they happen to do "git pull --rebase" most of the time. Would be nice if "git pull" simply did that by default. You can set that up for the master branch of all your repositories by:

git config --global branch.master.rebase true

Rewriting history

Rewriting history is possible as long as none of the commits involved have been pushed out to the central repository yet. Say, you spend an evening implementing a feature. The session may generate 10 commits. After you are done, you see that some of the previous commits were buggy and the newer commits fix bugs introduced by them. Those commits would look better if they didn't have the bug to begin with. Or you may want to reorder commits to group them more logically by what they do. Or you may just want to improve the commit messages before making the changes public. You are more than welcome to do all those as long as the commits involved are not out in the public repo yet, but after that, well, forget it :).

Amending last commit

If you committed something and have not pushed it yet, and you found a bug in your commit that you want to fix or simply want to change the commit message, or in any other way amend the commit, simply use the --amend option of "git commit". That is, to amend all your changes to the previous commit, do:

git commit --amend -a

and you get a chance to edit the commit message. If you just want to edit the commit message, a simple:

git commit --amend

will do.

Reshuffling multiple commits

The interactive rebase command is your friend. If you want to edit / reorder / squash-into-one the last five commits, do:

git rebase -i HEAD~5

That will bring up an editor page with the last five commits listed. You can reorder the lines to reorder the commits. Or change the word at the beginning of the line to get a chance to edit the commit, or squash it (ie. merge it) into the commit before it.

git goodness for bash users

Here's a few tips to improve your life if you bash as your main shell.

bash-completion for git

If you have ever seen bash-completion, chances are you are addicted to it. Search for a bash-completion package in your distribution and install it. For example, on Fedora the bash-completion package includes among other completions ones for git.

Alternatively, you can add bash-completion goodness for git by installing this completion script from the main git distribution. Try "locate git-completion" first to check if it already exists on your system, since it is provided by some distributions. You would still want to copy it to a different file that has a path you can rely on in that case. Include the path to the file you created with the script on your system in ~/.bashrc with

source <path-to-file>

git branches in shell prompt

Wouldn't it be rad if your shell prompt included the name of the currently checked-out branch? Well, if you enabled git bash completion as described above, you're just one step away! Try adding something like the following in your ~/.bashrc:

. /usr/share/git-core/contrib/completion/
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

You can even set the GIT_PS1_SHOWDIRTYSTATE environment variable to see if there are unstaged (*) and staged (+) changes in your branch by adding the following to ~/.bashrc:


You will need to do "source ~/.bashrc" and open a new terminal window to see the changes take effect.

Adjust to your taste. Enjoy!

git & bugzilla

Owen wrote a nifty tool called git-bz. See Owen's post about it. Note that the syntax has changed since that post, so you'll want to read the documentation at the top of the script file.

With this tool, you can attach patches to bugs, create bugs with patches, apply patches from bugs, etc. The header of the script contains all the information about it.

git GUIness

See Git/Tools.

git & Atom

Use the cgit URL that you would like to subscribe to in your feed reader, for example:

for the Atom feed of the master branch of GLib. Alternatively, your web browser should show any available feeds if you are viewing cgit commit logs.

Git/Tips (last edited 2013-11-13 12:40:27 by DavidKing)