useful git aliases

Thought I’d share some useful git aliases with people. These can go in your ~/.gitconfig.

[alias]
    branch-name = !git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/\\1/'
    export-branch = !git format-patch --stdout $1 > ~/Desktop/`git branch-name`.diff

git branch-name (based on an idea from Jon Maddox) will print the name of your current working branch. For example:

[danni@adelie empathy]$ git branch-name 
ft-warning-644062

This can then be used to create another alias, git export-branch which will export the current branch from the named branchpoint to a format-patch called ~/Desktop/branchname.diff. For example:

[danni@adelie empathy]$ git export-branch gnome-2-34
[danni@adelie empathy]$ ls ~/Desktop/*.diff
/home/danni/Desktop/ft-warning-644062.diff

Update: thanks to David in the comments for a much more reliable approach to get the branch-name:

branch-name = !git for-each-ref --format='%(refname:short)' `git symbolic-ref HEAD`

About Danielle

Danielle is an Australian software engineer, computer scientist and feminist. She doesn't really work on GNOME any more (sadly). Opinions and writing are solely her own and so not represent her employer, the GNOME Foundation, or anyone else but herself.
This entry was posted in Uncategorized. Bookmark the permalink.

9 Responses to useful git aliases

  1. Elijah Newren says:

    As git branch is ‘porcelain’, meaning its output is not stable, better would be something like:

    git symbolic-ref HEAD 2>/dev/null | sed -e s#refs/heads/##

    for getting the current branch name (both this command and yours produce an empty string when there is no current branch).

  2. David says:

    You can use plumbing commands instead:

    “git symbolic-ref HEAD” will give you the full name of the current branch.

    The only way to get safe short form of the branch name I know is this:

    git for-each-ref –format=”%(refname:short)” $(git symbolic-ref HEAD)

  3. Anonymous says:

    Apart from the comments above suggesting alternatives to git branch, I’d also suggest that you don’t need –no-color; people should never set any of the color options to “always”, only to “auto”.

  4. James Morris says:

    Try putting the branch name into your shell prompt. For bash (I can’t remember where this is from):

    parse_git_branch ()
    {
    ref=$(git-symbolic-ref HEAD 2> /dev/null) || return;
    echo “(“${ref#refs/heads/}”)”
    }

    PS1='[\u@\h \W $(parse_git_branch)] $ ‘

  5. Danielle says:

    @James: that’s very similar to the approach I linked to in my post only with the improvements pointed out by Elijah.

    http://www.jonmaddox.com/2008/03/13/show-your-git-branch-name-in-your-prompt/

  6. Danielle says:

    @David: this looks like a much better approach. I have updated my post with an adapted version that works in the ~/.gitconfig file.

  7. Francis says:

    Now that you’ve got a better way of doing it anyway, your sed command… you could’ve achieved the same with
    sed -ne ‘s/^* \\(.*\\)$/\\1/p’
    which would save you from deleting the pattern space for all but one line

  8. Rich Midwinter says:

    The JBoss guys have instructions for setting up a bash prompt to include the git branch and also indicates if you’ve made changes:

    http://community.jboss.org/wiki/HackingonAS7

  9. And sed -ne ‘s/^* //p’ is equivalent to sed -ne ’s/^* \\(.*\\)$/\\1/p’, except shorter, but I agree that relying on git-symbolic-ref etc. is more robust.

Comments are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>