
Let's say you have a git repo that contains two (or more) branches. The git branch --all command can be used to display the branches in the repo. In this example, there are two branches, feature/foo and feature/bar and feature/foo is the branch you are currently on.
~]# git branch --all
bar
* foo
master
remotes/origin/HEAD -> origin/master
remotes/origin/feature/bar
remotes/origin/feature/foo
remotes/origin/master
And let's say you are in the midst of updating a file in the feature/foo branch. The git status command can be used to see that the example.txt file in the feature/foo branch has modifications and uncommitted changes.
~]$ git status
On branch feature/foo
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: example.txt
no changes added to commit (use "git add" and/or "git commit -a")
And you need to switch over to the feature/bar branch and make a change and commit. If you attempt to switch to the feature/bar branch (or any other branch) something like this will be returned because you have uncommited changes to the example.txt file.
error: Your local changes to the following files would be overwritten by checkout:
example.txt
Please commit your changes or stash them before you switch branches.
Aborting
As the error suggest, you can either:
- Commit the changes you made to example.txt
- Stash the changes you made to example.txt (this article)
The git stash command can be used to stash the changes you made to example.txt in the currently selected branch feature/foo. Let's first check to see if there are any stashes.
~]$ git stash show
No stash entries found.
Assuming there are no stashes, let's use the git stash command to stash the changes made in the feature/foo branch.
~]$ git stash
Saved working directory and index state WIP on feature/foo: d17a5c0a initial commit
And now the git stash list command will show there is a stash.
~]$ git stash list
stash@{0}: WIP on feature/foo: d17a5c0a initial commit
And likewise the git stash show command will show that the changes made to example.txt in the feature/foo branch have been stashed.
~]$ git stash show
example.txt | 136 ++++++++++++++++++++++++++++------
1 file changed, 115 insertions(+), 21 deletions(-)
And now we can switch to some other branch.
~]$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Then, when you return to the feature/foo branch, you might notice that example.txt does not contain the changes to made to example.txt since the last commit. This is because the changes made to example.txt are still stashed. The git stash pop command can be used to bring example.txt back to the state it was in when you stashed it.
~]$ git stash pop
On branch feature/foo
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: example.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (fdd048a922d8bcfe32c6c74056956c79ac574971)
Did you find this article helpful?
If so, consider buying me a coffee over at