Wednesday, 10 May 2017

GIT Stash

Save temporary changes using the stash

Do not mix Git Stage (prepare your commit) with Git Stash (saving your modifications as patches in a branch-free vault)
First, add your file to the .git index (indexing the changes, not commiting nor stashing yet)
git add <filename>
Save your changes in the stash area; AND RESET SOURCES TO CLEAN CHECKOUT! (git reset --hard)
git stash                #Dirt, not saving message
git stash save <message> #Much verbose
git stash save --patch <message> #Interactive picking with changes to stash
List the saved stash files
git stash list  #List all in stash, with message and reference
git stash show  <reference> #Will show just the modified filename; with no reference shows last
git stash show -p <reference> #Will show the changes in diff/patch format; with no reference shows last
Stashes are always saved with references like stash@{<revision>} ; being {0} the latest revision and {1} the second, etc ({0} is the default to apply).
Reapply the stash to your code, in 3 different ways:
git stash apply <reference> #Will apply stashed change to your code, keeping the stash
git stash pop <reference> #Will apply stashed change to your code, removing from the stash
Convert your stash into a new branch
git stash branch <branchname> [<stash>] 
More recipes:
git stash list

# This will show something like:
stash@{0}: WIP on master: 6ebd0e2... My debug code
stash@{1}: WIP on master: 9cc0589... My stashed changes

# To see the details of an individual stash:
git stash show <optional stash ref>

# To see the stash at the top of the stack:
git stash show

# To see the second item from top:
git stash show stash@{1}

# To restore a stash while keeping a copy in the stack:
git stash apply <optional stash ref>

# To restore a stash and remove it from the stack:
git stash pop <optional stash ref>

# To remove a single stash from the stack without applying it:
git stash drop <optional stash ref>

# To clear out your entire stash stack:
git stash clear

No comments:

Post a Comment