Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash
command.
Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.
Stashing Your Work
$ git status
On branch optimize_add_surround_audio_test
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: app/src/androidTest/java/com/XXX/XXX/MainTest.java
Untracked files:
(use "git add <file>..." to include in what will be committed)
JenkinsRun.sh.bak
no changes added to commit (use "git add" and/or "git commit -a")
$ git stash
Saved working directory and index state WIP on optimize_add_surround_audio_test: 5509c63 Merge branch 'dev' of XXX/XXX into master
$ git status
On branch optimize_add_surround_audio_test
Untracked files:
(use "git add <file>..." to include in what will be committed)
JenkinsRun.sh.bak
nothing added to commit but untracked files present (use "git add" to track)
Then it is safe to switch to other branches without committing or losing anything.
$ git stash list
stash@{0}: WIP on optimize_add_surround_audio_test: 5509c63 Merge branch 'dev' of XXX/XXX into master
stash@{1}: WIP on optimize_failing_screenshot_capture: a72194b add suite logger
$ git stash show stash@{1}
.../com/mrvl/netflixwithopencv02/MainTest.java | 66 ++++++++++++++--------
.../java/com/mrvl/netflixwithopencv02/Methods.java | 47 ++++++++-------
2 files changed, 69 insertions(+), 44 deletions(-)
$ git stash drop stash@{1}
Dropped stash@{1} (fdee692a43f37fc38e4fd6bfe5587eec7696b7ee)
$ git stash list
stash@{0}: WIP on optimize_add_surround_audio_test: 5509c63 Merge branch 'dev' of XXX/XXX into master