Git Stash

Say you are halfway done with work on a branch, but an issue with “master” needs fixing NOW. You’re not quite ready to commit what you’re working on, but need to make a quick fix to master. Fear not, just stash your current changes so you can switch to master quick.

$ git stash save

Above command will save your modified files by “stashing” them away. And restoring your last commit to your working copy.

After you submit your fixes, and checked in your changes you can verify the status of your repository:

$ git diff
$ git status

this will result in:

nothing to commit (working directory clean)

Now you can resume work on the feature where you stopped befoe fixing the master. First list all the stashes to make sure you get the right one.

$ git stash list

As you can see, your stash is in the stash list. Let’s apply the stash so you can get back to work.

$ git stash apply

Ok, so now that you have all your stashed changes back, let’s clean up the stash list. Go ahead and drop any unwanted stashes.

$ git stash drop

Conflicts

If you get a conflict when trying to apply your previous stash, you can perform a hard reset back to the last commit.

$ git reset --hard HEAD

Now that you have the file reset, use the pop command to apply and delete the stash at the same time.

$ git stash pop

Keeping the index

For this you need to swap branches again, but this time some of your changes may ready to be committed. Stash all the unstaged changes, but leave the staged changes intact so they can be committed afterwards.

$ git stash save --keep-index

List Options

If there are so many changes, you may need to stash some more changes, but if it doesn’t seem to be working on these untracked files you just created, try using the –include-untracked option to stash all those new files.

$ git stash save --include-untracked

If you’ve been stashing stuff all day since you keep having to make small quick fixes to master. Then you will have a bunch of stashed changes and you may not be sure which one you need to apply. You could look through them all one by one, or you could use the –stat option to list information about each stash. Give that a try.

$ git stash list --stat

Stash Details

You now have a list of stashes that are showing you more information, and you think you may have spotted the stash that you need. Take a more detailed look at the stash@{2} stash with the –patch option.

Stash Message

You’re halfway through a large feature. Your team has decided that they want you to deploy what you have so far, but you have a smaller unfinished feature that isn’t ready to be committed yet. Go ahead and stash your current changes with the message Added method, so you know which stash has your changes.

$ git stash save "Added a method"

Stash Branching

Now that you’ve deployed the main part of your feature, you need to finish up what is left. Create a new branch and apply your most current stash all in one command.

$ git stash branch  stash@{0}

Be first to comment

Leave a Reply