Stash your changes and bring them back

Description: You edited app.txt but must switch tasks. Put the changes aside, check that the folder is clean, and then bring the changes back.

Hint:

  • Use git stash and git stash pop.

Solution

Terminal output

$ echo "half done" >> app.txt
$ git stash
Saved working directory and index state WIP on main: 6b3a366 Add app
$ git status -s
$ git stash pop
On branch 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: app.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0aac64f7d5bb638391472f0c078c4e2ca0506864)
$ cat app.txt
v1
half done

How it works

  • After git stash the folder is clean, so you can switch branches.
  • git stash pop applies the changes again and removes the stash entry.

Read Git Stash.


Learn more about the similar topics:
Tutorials
Git Restore
Git Commit Amend
Git Reset
Git Revert
Git Stash
Git Reflog
Exercises & Assignments
Discard the changes of a file
Undo the last commit but keep the changes
Revert a commit with git revert
Fix the message of the last commit
Stash your changes and bring them back
Recover a deleted branch with git reflog
Interview Questions & Answers
What is git revert?
How to undo last commit using git reset?
What is the difference between git reset and git revert?
What is the difference between soft, mixed and hard reset in Git?
What is git stash and when do you use it?
How do you change the last commit message in Git?
How do you squash commits in Git?