Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Better git stash
#!/usr/bin/env python2
import subprocess
import sys
import datetime
import re
if __name__ == "__main__":
t =
branchn = subprocess.check_output([ 'git'
, 'rev-parse'
, '--abbrev-ref'
, 'HEAD']
).split('\n') [0]
tempbranch = re.match('temporary-commits/(.*)', branchn)
if tempbranch:
if '-r' in sys.argv:
tbn =
print('Coming from temporary branch at '+tbn)
origBranch, tmpDate = re.match('(.*?)/([0-9]*-[0-9]*-[0-9]*.[0-9]*-[0-9]*-[0-9]*)$', tbn).groups()[ 'git', 'checkout', '--detach' ])[ 'git', 'reset', '--soft', 'HEAD^' ])[ 'git', 'checkout', '-b', origBranch ])
if '-r' not in sys.argv:[ 'git'
, 'checkout'
, '-b'
, 'temporary-commits/'+branchn+'/'+t.strftime('%Y-%m-%d_%H-%M-%S')
])[ 'git'
, 'commit'
, '-am'
, 'Temporary commit' if len(sys.argv) == 1
else sys.argv[1]

This comment has been minimized.

Copy link
Owner Author

leftaroundabout commented Mar 22, 2016

This script does much the same thing as git stash, but stores the information like any other commit in a dedicated git-branch, which makes the stash rather more easy to work with.


  • git-tmp-commit to stash some work as a commit on a suitably named temporary branch.
  • You can simply check out the branch like any other git branch. git-tmp-commit -r will “undo” a temporary commit, i.e. reset you to the previous “proper” branch, but keep the work as uncommitted (but staged) changes in the working directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.