Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

There is an urgent request comes in like bug fix and you have to switch to it but don't want to commit or lose your work in your current working branch. This is where git stash comes in handy. For more information, click git stash link.


Git stash


inanzzz@inanzzz:~/project$ git branch
develop
* my-feature-branch
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
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: file_one.txt
modified: file_two.txt

no changes added to commit (use "git add" and/or "git commit -a")
inanzzz@inanzzz:~/project$ git stash
Saved working directory and index state WIP on my-feature-branch: 860fcb2 Commit
HEAD is now at 860fcb2 Commit
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
nothing to commit, working directory clean

Do other urgent work


Checkout to "develop" and do what ever you have to do. You may create a branch to work etc. and push it.


inanzzz@inanzzz:~/project$ git checkout develop
Switched to branch 'develop'
inanzzz@inanzzz:~/project$ git branch
* develop
my-feature-branch

Go back to original branch


We're back where we were and recover from git stash with git stash apply.


inanzzz@inanzzz:~/project$ git checkout my-feature-branch 
Switched to branch 'my-feature-branch'
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
nothing to commit, working directory clean
inanzzz@inanzzz:~/project$ git stash apply
On branch my-feature-branch
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: file_one.txt
modified: file_two.txt

no changes added to commit (use "git add" and/or "git commit -a")