Git - Stash Operation
Git - Stash Operation
Section titled “Git - Stash Operation”Imagine you’re working on a new feature, and your code is in an intermediate, uncommitted state. Suddenly, an urgent bug fix is required on a different branch. You can’t commit your half-finished work, nor do you want to lose your changes. This is where git stash comes in handy.
The git stash command takes your modified tracked files and staged changes, saves them in a temporary storage area (the “stash stack”), and reverts your working directory to the state of the last commit (HEAD). This allows you to switch branches or work on something else without committing incomplete changes.
Suppose Jerry is modifying string_utils.c:
[jerry@devbox project]$ git status -s M string_utils.c?? new_temp_file.txtstring_utils.c is tracked and modified. new_temp_file.txt is untracked. By default, git stash only stashes changes to tracked files. To include untracked files, use git stash -u or git stash --include-untracked. To stash staged changes as well, git stash automatically includes them.
Jerry needs to switch branches for an urgent task but doesn’t want to commit his current work. He stashes his changes:
[jerry@devbox project]$ git stash push -m "WIP: new string parsing feature"(Using git stash push -m "message" is a modern way to stash with a descriptive message. git stash alone also works.)
Saved working directory and index state On main: WIP: new string parsing featureHEAD is now at jkl3456 Implement my_strcpy function and add testNow, Jerry’s working directory is clean (matching the HEAD commit). The modified string_utils.c is reverted, and if he used -u, new_temp_file.txt would also be gone from the working directory (but saved in the stash).
[jerry@devbox project]$ git status -s(If new_temp_file.txt was not stashed with -u, it would still show as ?? new_temp_file.txt)
Jerry can now safely switch branches. To see the list of stashed changes, he uses git stash list:
[jerry@devbox project]$ git stash liststash@{0}: On main: WIP: new string parsing featureAfter completing the urgent task and returning to his feature branch (or main in this case), Jerry wants to reapply his stashed changes. He uses git stash pop:
[jerry@devbox project]$ git stash popgit stash pop reapplies the most recent stash (stash@{0}) and, if successful, removes it from the stash stack. The output will show the re-applied changes:
On branch mainYour branch is up to date with 'origin/main'.
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: string_utils.c
Untracked files: (use "git add <file>..." to include in what will be committed) new_temp_file.txt (if it was stashed with -u and is now restored)
no changes added to commit (use "git add" and/or "git commit -a")Dropped refs/stash@{0} (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)His working directory is now back to the state it was in before stashing.
[jerry@devbox project]$ git status -s M string_utils.c?? new_temp_file.txt (if restored)If git stash pop encounters conflicts, it will reapply the changes but won’t drop the stash, allowing you to resolve conflicts and then manually git stash drop stash@{0}. Alternatively, git stash apply reapplies the stash but leaves it on the stack, which can be useful if you want to apply the same stash to multiple branches.