Git – Tips and Tricks

There are a lot of things that could make our life easier as a developer. Small things with a major impact, simple tricks that save us a few seconds on the most recurring tasks or even a change in our workspace that could drive us out of the basic routine.

Since Git is one of the most used revision control and source code management system, let’s start empowering it by learning some simple tricks:

What are the most important Git Tips and Tricks?

» Auto-Completion

If you’re using Bash shell, you may not know, but Git actually comes with a script that enables auto-completion. So, download it from:
https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
and copy it to your home directory. Afterwards, add this to your .bashrc file:

If you want to set up Git to automatically have Bash shell completion for all users, copy this script to the /opt/local/etc/bash_completion.d directory on Mac systems or to the /etc/bash_completion.d/ directory on Linux systems. This is a directory of scripts that Bash will automatically load to provide shell completions.

If you’re using Windows with Git Bash, which is the default when installing Git on Windows with msysGit, auto-completion should be preconfigured.

Press the Tab key when you’re writing a Git command, and it should return a set of suggestions for you to pick from:


» Git Aliases

Git doesn’t infer your command if you type it in partially. If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using git config. Here are a couple of examples you may want to set up:

This means that, for example, instead of typing git commit, you just need to type git ci. As you go on using Git, you’ll probably use other commands frequently as well; in this case, don’t hesitate to create new aliases.

This technique can also be very useful in creating commands that you think should exist. For example, to correct the usability problem you encountered with unstaging a file, you can add your own unstage alias to Git:

This makes the following two commands equivalent:

This seems a bit clearer. It’s also common to add a last command, like this:

This way, you can see the last commit easily:

Just don’t play too much with the aliases. Who knows, you may end-up on a computer without your aliases and all the time saved by using aliases will be spent remembering the actual commands :)

» Mistyping commands
You have clumsy fingers or try writing fast and you end-up mistyping the intended command? Yeah, as you’ve probably guessed there’s a solution for that, and it’s actually a config option:

The result is pretty handy:


» Awesome prompt

If you find yourself in the situation where you constantly switch between branches and you end-up screwing things up just because you forgot the current one, having a great prompt is quite useful to know what is the current branch you’re working on, if you have modified some files, if you have some commits to push or to pull from the server, and so on.

The prompt created by Tung Nguyen is one of the best created so far and it will display you the following:

  • username@host
  • current path
  • current branch
  • a lightning if you have some modified files in your working directory or in your staging area, waiting to be packed in a commit
  • an up, down or up-and-down arrow if you have commits to push or pull from remote repository

You can download it from here, and save it in a file, let’s say ~/git-prompt.bash. Now, add the following line to your .bashrc:


» Disable space changes

When spaces are added in a line, the git diff command displays the line as changed. When you read your changes before creating a commit, this can be annoying to understand the diff, especially when the spaces have been added/removed by your IDE (useless spaces, fix indentation, replace tab by spaces, etc.).

To avoid this pollution in the git diff, you can add the -w option to omit spaces (and tabs) changes.

Let’s take an example:

What are the important updates in this piece of code? It’s quite easy to check in this example but imagine a big source file … reformatted. It’s horrible going through the entire file to see where’s the change. But with the -w option:

Are you checking the changes in Github? Add ?w=1 to the URL to see the diff with whitespace ignored!