5 rules to Git better

posted by alexis reigel on november 10, 2016

New post on the Panter Blog (the company I’m working for):

blog.panter.ch/2016/08/31/5-rules-to-git-better.html

git bash prompt

posted by alexis reigel on july 05, 2013

Hi.

I tried many different bash git prompt solutions, all of which were either too complicated, too buggy or too ugly. So…

I didn’t want to invent too much myself, so I used the git prompt provided by git as a basis. This script does all the magic already, so the only thing left to do was to make it look nicer. This is the whole section in my .bash_profile:

# bash git prompt
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="verbose"

git_current_branch_name="\$(__git_ps1 '%s' | sed 's/ .\+//' | sed -e 's/[\\\\/&]/\\\\\\\\&/g')"
git_status_substitutes=(
    "s/$git_current_branch_name//;" # remove branch temporarily
    "s/u//;" # upstream
    "s/+\([0-9]\+\)/▴\1/;" # outgoing
    "s/-\([0-9]\+\)/▾\1/;" # incoming
    "s/%/?/;" # untracked
    "s/+/✓/;" # staged
    "s/*/✕/;" # unstaged
    "s/\(.\+\)/($git_current_branch_name\1)/;" # insert branch again
)
git_status_command="\$(__git_ps1 '%s'| sed \"${git_status_substitutes[@]}\")"

if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+($debian_chroot)}\[\033[0;37m\] \w \[\033[34m\]$git_status_command\[\033[37m\]\$\[\033[00m\] "
else
PS1="${debian_chroot:+($debian_chroot)} \w $git_status_command\$ "
fi
unset git_status_substitutes git_status_command git_current_branch_name

And now, step by step:

GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="verbose"
git_current_branch_name="\$(__git_ps1 '%s' | sed 's/ .\+//;s/(//')"
git_status_substitutes=(
    "s/$git_current_branch_name //;" # remove branch temporarily
    "s/u//;" # upstream
    "s/+\([0-9]\+\)/▴\1/;" # outgoing
    "s/-\([0-9]\+\)/▾\1/;" # incoming
    "s/%/?/;" # untracked
    "s/+/✓/;" # staged
    "s/*/✕/;" # unstaged
    "s/\(.\+\)/($git_current_branch_name \1)/;" # insert branch again
)
git_status_command="\$(__git_ps1 '%s'| sed \"${git_status_substitutes[@]}\")"
if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[0;37m\] \w \[\033[34m\]$git_status_command\[\033[37m\]\$\[\033[00m\] "
else
    PS1="${debian_chroot:+($debian_chroot)} \w $git_status_command\$ "
fi

The final output looks like this:

~/src/dotfiles (master ✕✓? ▴1▾5)$

The original output of __git_ps1 looks like this, which is much more cryptic:

~/src/dotfiles (master *+% u+1-5)$

The meaning of the symbols:

Unstaged changes
Staged changes
? Untracked files
▴1 One changeset ahead of remote
▾5 Five changesets behind remote
= No difference to remote

You can see the whole thing in action in my .bash_profile.

reminder. git. awesome.

posted by alexis reigel on july 19, 2012

This is just a quick reminder of how awesome git actually is. The story is short:

My disk crashed and I lost a couple of local commits that I hadn’t pushed (to github) yet. Luckily, I deployed them to the staging server already (actually, this is a reminder of how awesome git and capistrano actually are). Because capistrano is awesome and deploys the git repo to the server, and git is awesome too, I could restore my local repo from the staging server:

local:  $ ssh staging_server
server: $ # some `cd` action
server: $ tar czf asdf.tgz www_folder
server: $ # ctrl + D
local:  $ cd /tmp
local:  $ scp user@staging_server:/tmp/asdf.tgz .
local:  $ tar xf asdf.tgz
local:  $ cd my_local_repo
local:  $ git pull /tmp/asdf dev

A simple git pull from the copied repo and I’m all set again.

How awesome is your dev stack?