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