My git tips...

Submitted by gwolf on Sat, 12/13/2008 - 19:35

Ok, so a handy meme is loose: Handy Git tips. We even had a crazy anatidae requesting us to post this to the Git wiki whatever we send on this regard to our personal blogs.
Following Damog's post, I will also put my .bashrc snippet:

  1. parse_git_branch() {
  2. branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
  3. if [ ! -z "$branch" ]
  4. then
  5. if ! git status|grep 'nothing to commit .working directory clean' 2>&1 > /dev/null
  6. then
  7. branch="${branch}*"
  8. mod=`git ls-files -m --exclude-standard|wc -l`
  9. new=`git ls-files -o --exclude-standard|wc -l`
  10. del=`git ls-files -d --exclude-standard|wc -l`
  11. if [ $mod != 0 ]; then branch="${branch}${mod}M"; fi
  12. if [ $new != 0 ]; then branch="${branch}${new}N"; fi
  13. if [ $del != 0 ]; then branch="${branch}${del}D"; fi
  14.  
  15. fi
  16. fi
  17. echo $branch
  18. }

This gives me the following information on my shell prompt:

  • The git branch where we are standing
  • If it has any uncommitted changes, a * is displayed next to it
  • If there are changes not checked in to the index, M (modified), N (new) or D (deleted) is displayed, together with the number of files in said condition. i.e.,

    Sometimes, entering a very large git tree takes a second or two... But once it has run once, it goes on quite smoothly.
    Of course, I still have this also in .bashrc - but its funcionality pales in comparison:
    1. get_svn_revision() {
    2. if [ -d .svn ]
    3. then
    4. svn info | grep ^Revision | cut -f 2 -d ' '
    5. fi
    6. }

    I am sure it can be expanded, of course - but why? :)
AttachmentSize
gitprompt.png5.31 KB
( categories: )
alexandrul's picture

I've added a line to

I've added a line to substract the number of deleted files from the modified ones:

del=`git ls-files -d --exclude-standard|wc -l`
let "mod = ${mod} - ${del}"
if [ $mod != 0 ]; then branch="${branch}${mod}M"; fi

gwolf's picture

Thanks, that adds some consistency

Although a removal _is_ a modification, the resulting behaviour is closer to reality :) Thanks!

gwolf's picture

It is easier if you reorder the lines

I have reordered the snippet incorporating your idea:

  1. branch="${branch}*"
  2. new=`git ls-files -o $git_opts|wc -l`
  3. del=`git ls-files -d $git_opts|wc -l`
  4. mod=$(( `git ls-files -m $git_opts|wc -l` - $del ))
  5. if [ $mod != 0 ]; then branch="${branch}${mod}M"; fi
  6. if [ $new != 0 ]; then branch="${branch}${new}N"; fi
  7. if [ $del != 0 ]; then branch="${branch}${del}D"; fi

Heh, it's incredible how you get so used to something you don't notice how it breaks reality ;-)

Thanks!