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`

Author: 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.

9 thoughts on “useful git aliases”

  1. 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. 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. 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. 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. @David: this looks like a much better approach. I have updated my post with an adapted version that works in the ~/.gitconfig file.

  6. 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

  7. 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.

Leave a Reply

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

Creative Commons Attribution-ShareAlike 2.5 Australia
This work by Danielle Madeley is licensed under a Creative Commons Attribution-ShareAlike 2.5 Australia.