# Git tips that I learned from the Master

October 8th, 2010 by
| Reply

This week I visited the JAOO or GOTO; conference as its name nowadays is. I went to a lot of presentations, among them was Mastering Git Basics by Tom Preston-Werner. I am not really a Git veteran, but I do use it on a regular basis. Therefore I was anxious to get more knowledge and to walk away with some tips.

It was a very nice presentation and I did learn a few tricks, in this short blogpost I will share these tricks with you.

All the tricks in this blog are coming from the presentation from Tom but a lot of documentation is publicly available. Sometimes I'll refer to these online resources.

## Colors and branches in your shell

During his presentation Tom used the command prompt for all the git commands. He had some nice colors in the prompt as well as in the git output. There is not a lot you need to do to make the shell (on mac-osx in my case) look like this:

Notice that the output of git status uses colors. Green for files in the index that are ready to be committed. Red for files that have changed in your workspace but are not added to the index. I personally like the use of colors in the command prompt as well as the name of the branch your workspace is pointing to. What do you need to do to accomplish this?

First thing to accomplish is the colors in the git output, you can do that with one command:

git config --global color.ui true


Next up, the colors and the name of the branch in the command prompt. Tom did show the colors and the name of the branch, but he told us to use google to find out how to do it. As I always do what people tell me, I used google and I found this blog post by Jon Maddox that gave me enough information to create my own command prompt configuration. Below you can see what I put in my .bash_profile. I work on a Mac and I use white shells, do take that in mind when using this for yourself.

function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* $.*$/(\1)/'
}

function proml {
local       BLACK="\033[0;30m"
local        BLUE="\033[0;34m"
local       GREEN="\033[0;32m"

PS1="$BLUE[\w$GREEN\$(parse_git_branch)$BLUE]\
$BLACK\$ "
PS2='> '
PS4='+ '
}
proml


## Diffs

Git stores your files in different places. In general you have three important storages. You have commit(s), the index and your workspace. In short, your workspace is where you make changes. The index is where you prepare your commit. The commit is what you can share. You can use the diff command to check the differences between these different storages. To learn everything about the command, use git help diff.

First we do a diff between an item in the working copy and the index. The following image shows that for one file. Nice to have these colors, or not?

If you want to see the difference between a file in the index and that file in the latest commit, you can use the --staged flag.

git diff --staged app/src/main/resources/log4j.properties


Using diff it is also very easy to check the changes between two branches. using git diff master otherbranch shows the difference between the two branches. It is also possible to get a more higher level. If you just want to see the files and if they have been removed, added or changed, you can use: git diff master otherbranch --name-status

There are a lot of things that you can diff, find out more on the refpage.

## Adding files to the index

Adding a file to the index is easy, just use git add <filename> and you are done. There are easier ways though, especially if you have more files that you would like to add. One of them is using wildcarts, the other one is to interactively handle all changes in your workspace.

### Wildcarts

You can use git add . to add all files recursively. When you wish to add all files in a directory you can use the git add header/* command and wildcart

### Doing it interactively

Another option I like a lot is the git add -p. This command takes all changes of your working directory and asks you per change what to do with it. The following screen shows you the first item of my changes. This command is a shortcut to the Interactive Mode of git.

Look at the last line, you have a lot of options. The most basic ones are y and n. But what are all the others. I found the following explanation in the help for git add. So after typing git help add, scroll down to the Interactive Mode explanation and you can find it.

y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help


As you can see this is a very powerful feature that will make you use the command line more and more

One remark, git add -p does not work for new files. These are the ones you have to add to the index first by hand.

## Branching

One of the most powerful features of Git is the way it makes use of branches. If you want to learn how to use branches, use the reference page. Some of the things I found useful are:

• Deleting branches is safe to do, the only thing you delete is a pointer to a commit. So you are not trowing away code.
• git remote -v, shows the fetch and pull remotes for your branch.
• git branch -a, shows you a list of remote as well as local branches.
• git push origin otherbranch, pushes your local branch named otherbranch to the remote repository.
• ?

## Concluding

It was a very interesting presentation, certainly made me more comfortable with git. Hope some of the things I learned are useful to others as well.

If you came to this post to learn everything about git and you feel like this post did not teach you a lot (which I can imagine), use the following resources.