Show your Git Branch Name in your GNU Screen Status Line
Git Branch Prompt
Many people have written about showing the current git branch in your shell prompt.
There is even the Bash function __git_ps1 provided in the main Git repo.
Screen Status
As I always use screen, I decided to take a different route: put the current branch name in the screen status line.
Changing the Screen Current Directory
When you start screen, it remembers the current directory. Issuing 'cd' commands inside a shell does not change the value. The only way to change it is to issue a screen chdir command.
In order to know the current branch in a particular screen window, I needed to make screen's current directory change to follow the shell it is presenting.
I added the following to my .bashrc:
function set_screen_path() {
screen -X chdir "`pwd`"
}
case $TERM in
screen*)
PROMPT_COMMAND=set_screen_path
;;
esac
This is Bash specific, but similar code should work in other shells.
git-current-branch
Next, I created the following shell script as 'git-current-branch' in my path:
#!/bin/sh git branch 2>/dev/null | grep '*' | sed 's/\* //'
.screenrc
And finally, I added a 'backtick' command to my .screenrc file.
Here is a stripped down version:
startup_message off
backtick 1 0 1 git-current-branch
hardstatus alwayslastline "%-w%{.bw}%n %t%{-}%+w %-45= %1`"
screen -t bash 0
screen -t edit 1
select 0
The important parts are:
backtick 1 0 1 git-current-branch
which creates a 'backtick command' (number 1) which calls git-current-branch once a second.
%1`
which inserts the output of backtick command 1 into the status line.
The Result

Conclusion
The system is not perfect as the shell only notifies screen of its directory when a new prompt is created.
The result is that if you switch screens, the status line doesn't get updated until after you issue a command.