Git tips and tricks

Though a lot of the Drupal community is using Git, I don't think everyone knows the tools I want to write about in this post. I can tell you, I cannot live without theme anymore:

  • Git flow
  • Git completion
  • Bash prompt with Git information
     

Git flow

Many of the newcomers to Git are struggling with the way to use branches for development, releases and hot-fixes. There is a great blog post of Vincent Driessen about this branching model that he calls Git flow. He developed a bunch of Git extensions to implement this branching model in Git. You can download those on Github. With this branching model it is very easy to create new feature or release branches without any effort or thinking. Just type:

git flow feature start myfeature

and start coding in your new feature branch. If the feature is to your liking and you want to push it back in the main develop branch just type

git flow feature finish myfeature

and Git flow will take care of the merging and deleting of the tag. The nice thing is, that you can start a new feature or release on any branch. There is a lot more to it, but I suggest just reading the blog post of Vincent Driessen to learn all about it. We are using this workflow at Triquanta now for some time and we can really recommend it.

Git completion

if you spend a lot of time in the shell as I do, it is very handy that you have auto completion for all sorts of things. I use bash and will show you what I use for auto completion for Git. If you use another shell it will not be that hard to adjust it for your environment.

I have 2 files in a folder called bash-completion.d in my home folder:

  • git-completion.bash  
  • git-flow.bash

In my .bashrc file I have a line that loads those files

for f in ~/bash_completion.d/*; do source $f; done

The files are attached to this post. These will give you really cool auto completion for Git and for Git flow. You also get branch name completion, which is very handy if you forgot how you named your feature branch exactly.

Bash prompt

If I'm working in the shell I like it when my prompt gives me some information about where I am and what I'm doing. I have some lines in my .bashrc that will give me a prompt like this:

How handy is that! It will tell me on which branch I am, if there are staged (+) or unstaged (*) files etc. The functionality for this is also in the git-completion.bash. So if you installed that file, you will get this too Just paste the following code in your .bashrc in your home folder to get it:

PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ ';;

In the git-completion.bash file there is good documentation of the variables you can set to adjust what kind of information you see in your prompt. You can show if there are any untracked files, or if you are ahead or behind upstream for example.

If you have more tips on Git I would love to hear them of course.