GIT - 23 Backing up work in progress with storage

This lesson explores Git's stash feature, which is designed to safely set aside work-in-progress changes you do not want to commit yet. The starting point is a clean repository on master. We open coucou.html, add a couple of lines, save and close. git status now reports the file as modified.

Stashing work in progress

Imagine you suddenly realize you shouldn't be working on this right now — maybe you started on the wrong branch, or you need to switch to fix something more urgent first. Instead of losing your changes or committing them prematurely, you stash them:

git stash

Git replies that HEAD has been saved — meaning the latest commit on the current branch, here master — and that the modifications are now stored in a "work in progress" entry. You can list the saved entries:

git stash list

The output shows the stash entries: a WIP entry on master referring to the latest commit and its message. Running git status right after the stash shows we are back to a clean working directory: it is as if we never made the modifications.

Now we can work on something else without interference. We open venus, add a line such as v.3, save and commit it normally with git commit -am "venus update". The interruption is handled, and the working directory is clean again.

Reapplying the stashed work with git stash pop

Time to get back the earlier changes:

git stash pop

This is a two-in-one command: it applies the latest stash entry to the working directory and then drops that entry from the stash list. In our case, the modifications we made to coucou.html reappear. Running git stash list now returns no results — the entry has been consumed. We can verify in the editor that the file content matches what we had before the stash. A final git commit -am "coucou update" records the change cleanly. git stash apply works similarly but does not drop the entry — useful if you want to apply the same WIP in several places.