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")