If you are a developer who is using git, you surely may have encountered this error a lot of times.
Your local changes to the following files would be overwritten by checkout. Please commit your changes or stash them before you switch branches.
git stash
command.CASE 1:
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?
CASE 2:
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?
CASE 3:
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
"Stash the changes in a dirty working directory away".
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
git stash
, it is equivalent to calling the git stash push
command.To retrieve the list of all the saved stash entries, we can use the command
git stash list
stash@{0}
is the most recently created stash, and stash@{1}
is the one you created before it. WIP on master
means? 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 save
command 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.
SYNTAX:
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.
git stash pop stash@{n}
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 flag
with 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 drop
command, 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 clear
command for the same.You May Also Like To Read
BLUECAST TECHNOLOGIES PTE LTD,
31 SIMEI RISE, 528779 SINGAPORE
BLUECAST TECHNOLOGIES LLC P.O. Box: 128274, DUBAI
UNITED ARAB EMIRATES
BLUECAST TECHNOLOGIES PVT. LTD, KAKKANAD P.O.
KOCHI, 682030, INDIA
Email : info@bluecast.ae
Phone: +91 830 400 8000