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.
Summary
This lesson covers Git's stash feature, which allows developers to temporarily save work-in-progress changes without committing them. The tutorial demonstrates how to use `git stash` to store modifications, `git stash list` to view saved stashes, and `git stash pop` to restore those changes later, enabling seamless context switching between tasks.
Key points
- git stash saves uncommitted changes to a temporary storage area, allowing you to switch tasks without losing work
- git stash list displays all saved stashes with their associated commit messages and branch information
- git stash pop applies the most recent stash to your working directory and removes it from the stash list
- Stashing is useful when you need to pause work on one feature to handle an interruption or switch to another task
- Changes applied via git stash pop integrate back into the working directory and can be committed like normal edits
FAQ
What is the difference between git stash and git commit?
Git stash temporarily saves uncommitted changes without creating a permanent commit history entry, whereas git commit creates a permanent record in your repository. Use stash for temporary pauses and commit for finalized work you want to keep in history.
Can you recover a stashed change after applying it with git stash pop?
Yes, git maintains a reference log. If a stash is accidentally removed, you can usually recover it using git reflog or by referring to the stash reference before it was deleted.
How do you apply a specific stash instead of the most recent one?
Use `git stash list` to identify the stash you want, then apply it with `git stash pop stash@{n}` where n is the index number of the stash you wish to restore.