Wednesday, February 18, 2009

Git tab completion from Cygwin

I have been a fan of Git since starting to use it regularly and am going to give a talk on it at a tech lunch for my company in April. For the last two weeks, I have been using Git as an SVN front-end (my company has a backed-up SVN repo that I am using for SCM) while developing for a Windows target platform application. So far the experience with Git from cygwin has been a bit mixed. Another developer I am working with ran into problems with Git from cygwin (as opposed to msysgit) that I was unable to reproduce. His problem prevented him from being able to do simple commits on staged files. Bummer.

The little tidbit I wanted to document here is related to getting tab-completion of git commands working from the Cygwin bash shell. There are plenty of posts that already document how to do this, but cygwin kept complaining about having extra tokens in the shell scripts I was sourcing from my .bash_profile. After a little bit of forensic googling, I saw a post explaining that Windows line feed characters inserted by the text editor I was using on my shell scripts was causing my problem and that I should run the scripts through the d2u utility. Problem solved!

So for Windows folks running git from cygwin, here's how you get the awesomeness of tab-completion for git:

  1. Download the source for the version of git you have installed from http://www.kernel.org/pub/software/scm/git/
  2. Copy the git-completion.bash file from the contrib/ directory and paste it into your home dir -- (I renamed my file .git-completion.bash).
  3. Run that file through dlu: $:> d2u .git-completion.bash
  4. Source the file from your .bash_profile (in your cygwin home directory):
    if [ -e "${HOME}/.git-completion.bash" ] ; then
    source "${HOME}"/.git-completion.bash
    fi
  5. Restart your cygwin shell and you should be have git tab completion working. I can't say enough about how helpful tab completion is for git (or how smart the shell script is - truly is teh awesome)

9 comments:

Anonymous said...

hey, thanks for the tip
BUT I can't find the .git-completion.bash, could you give the name of the original file in contrib/

Thanks a lot!

Anonymous said...

ok, never mind, I found it.
\contrib\completion\git-completion.bash

Anonymous said...

Thanks, terribly helpful.

Arild said...

Thank you, very useful :)

sabgenton said...

For msysGit open git bash (usually right click explore and it's there)

cp /etc/git-completion.bash /c/cygwin/home/bob/.git-completion.bash

You must install msysGit with the option to have git in CMD

And the trick is in bash_proflie :
alias git=git.cmd

Reopen the shell :D

Anonymous said...

Thanks sabgenton. You really helped me as kernel.org is still down.

generic viagra review said...

Hello, i think that this post is very good, i would like to read more about it

luxury hotels new york city said...

Fabulous! I expect reading more, and getting a much more active component in the discussions here, when picking up a little knowledge. thank you for sharing this useful information and i will let know my friends as well.

Anonymous said...

Completion is now available in Cygwin as a package. So, all you have to do is to open the setup and install it via the "package manager".