If you are a developer who is using git, you surely may have encountered this error a lot of times.
You are in the middle of something, and you have got a call to urgently resolve a bug. (Thanks to your boss). So now you need to switch to a different branch and work on the bug fix. But you have changes you have not committed and you are not yet ready to commit this. What will you do?
You have uncommitted files and try to “git pull” and due to conflicts with the remote changes, git may refuse sometimes to overwrite.
Your local changes to the following files would be overwritten by merge. Please commit your changes or stash them before you merge.
This same type of error may also occur during a merge request, while pulling the latest changes from the remote branch.
How will you resolve this scenario?
You had committed to the wrong branch !!! (Seriously, sometimes we make mistakes. Who hasn’t?)
“git stash” could come for your rescue in most of these cases.
According to git-scm documentation, git-stash command is used to
Which means you could throw away your ‘uncommitted changes’ when you don’t want to commit those, in a temporary location (in an intention to take it later). To be precise, your modified ‘tracked’ files and staged changes – will get saved to a ‘stack’ and you can apply these changes later.
We can ‘stash’ the changes by simply executing the following command
git stash, it is equivalent to calling the
git stash pushcommand.
To retrieve the list of all the saved stash entries, we can use the command
git stash list
WIP on mastermeans? Because, you stashed those changes when you where on master branch. By default, a stash will get listed as ‘WIP on branchname’ which will be easier to identify it with respective to the branch you stashed it. Also, these are stashed after the commit –
825e72d initial commit
refs/stash. And the subsequent pushes will be available in the reflog of this reference.
When you list the stashes using git stash list command, isn’t it confusing to identify a stash, if there are many stashes happened after a commit? What if there is a way to give a name or message along with it when you stash them? Let’s see that in action.
The syntax for saving a name or message along with the stash is as follows.
git stash push -m <name or message>
git stash savecommand has been deprecated in favour of
git stash push. Anyway, it’s also easier to remember push and pop than a command like ‘save’ when dealing with git-stash which gives a ‘stack’ like storage. Am I right?
You had stashed away your changes and also listed those stash entries. But how do you take back the stash you saved? Is there any git unstash command? Or how do you undo git stash you just did stash? Just ‘pop’ that from the stash, like you pushed.
git stash pop
By default, this will retrieve the ‘last stashed entry’. What if you wanted to retrieve previously added stash entry or a specific stash?
You can use the following command to pop a specific stash entry, where ‘n’ is the stash index.
Or more simply,
git stash pop n
[Note:- Some shells would give error using curly brackets and so they have to be put inside quotes. So its better to use the second command.]
git stash apply is similar to pop, but the difference is:
git stash pop– throws away the stash entry from the stash-list after ‘applying it’, whereas
git stash apply– keeps it in the stash-list even after applying it, for later reuse.
(I can’t find a reason, why would you need to keep stash entry for later reuse. But anyway some people may prefer that instead of pop).
If you have multiple files modified, and you want to git stash a single file alone, mentioning the <path> of the file along with the git stash push command would be enough.
SYNTAX for git stash single file:
git stash push -m <message> <path-of-file>
Same like above, if you have multiple files to be stashed selectively, mentioning all the files’ path which you required to be stashed, along with the git stash push command would do the job.
git stash push -m <message> <path-of-file1> <path-of-file2>
--all flagwith the stash command.
git stash --all
What if you just need to sneek peek the contents of a stash entry without applying it? Or to know what files will get changed by applying a stash without applying it?
git stash show– will show the list of files changed in ‘last’ stash.
git stash show n– will show the list of files changed in ‘n th’ stash.
By adding a -p flag we can preview the contents too.
git stash show -p– will preview the git diff contents of the ‘last’ stash.
git stash show -p n-will preview the git diff contents in the ‘n th’ stash.
When you feel that you no longer require to keep a stash entry in the stash-list, you can delete the stash by executing
git stash dropcommand, which will remove the latest stash entry from the stash list. You can also mention the stash-index used like before to remove a particular stash entry from the stash-list.
git stash clearcommand for the same.