eagleas ([info]eagleas) wrote,

Eric Goodwin: bash auto-completion with git



You can simply download ~/.git-completion.bash from github source

DISCLAIMER: This is copy of Eric Goodwin original article

It may be a little hidden but Git actually comes with auto completion, you just have to set it up.

If you have the Git source on your computer then you are good to go. If not, you are going to have to download it. After downloading Git we are going to copy the git-completion bash file from the contrib/completion directory into our home directory, prepending it with a period so that it's hidden.
~ > git clone git://git.kernel.org/pub/scm/git/git.git git
~ > cp git/contrib/completion/git-completion.bash ~/.git-completion.bash


Depending on how you've setup your bash startup files the next couple of steps may differ. If you want a great guide on setting up your bash startup files, check out this article.

The first thing we are going to do is include git-completion.bash in our ~/.bash_profile. Look for the .bashrc source include and put git-completion right before it.
# ~/.bash_profile
source ~/.git-completion.bash
source ~/.bashrc

Now you just need to reload the bash_profile and auto complete is ready to go.
~ > . ~/.bash_profile

Wasn't that easy. While we're messing around in the bash startup files though, why don't we add another great little feature. Git-completion.bash includes a method (__git_ps1) to find your current branch and print out it's name. We are going to append the branch name to our command prompt so we can always tell what branch we are working in.

Open your .bashrc files and search for the 'export PS1' line and replace it with the following.
# ~/.bashrc
export PS1='\w$(__git_ps1 "(%s)") > '

After reloading your ~/.bashrc you will have your branch names showing up in the command prompt whenever you are in a Git project. No need to call git-branch to see what branch you are in anymore.
~/projects/TestGit(master) > git checkout rails20 
Switched to branch "rails20"
~/projects/TestGit(rails20) >

If you run into any trouble, check out the git-completion.bash file. It has some good instructions on how to use it at the top of the file.
Tags: auto-completion, bash, git

  • Post a new comment

    Error

    Your IP address will be recorded 

  • 0 comments
Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…