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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.