Skip to content

Instantly share code, notes, and snippets.

@leftaroundabout
Created March 22, 2016 13:11
Show Gist options
  • Save leftaroundabout/e939a9e46d278237aa9b to your computer and use it in GitHub Desktop.
Save leftaroundabout/e939a9e46d278237aa9b to your computer and use it in GitHub Desktop.
Better git stash
#!/usr/bin/env python2
import subprocess
import sys
import datetime
import re
if __name__ == "__main__":
t = datetime.datetime.now()
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 = tempbranch.group(1)
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()
subprocess.call([ 'git', 'checkout', '--detach' ])
subprocess.call([ 'git', 'reset', '--soft', 'HEAD^' ])
subprocess.call([ 'git', 'checkout', '-b', origBranch ])
else:
if '-r' not in sys.argv:
subprocess.call([ 'git'
, 'checkout'
, '-b'
, 'temporary-commits/'+branchn+'/'+t.strftime('%Y-%m-%d_%H-%M-%S')
])
subprocess.call([ 'git'
, 'commit'
, '-am'
, 'Temporary commit' if len(sys.argv) == 1
else sys.argv[1]
])
@leftaroundabout
Copy link
Author

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.

Usage:

  • 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