Stashing
Git stash allows you to temporarily save uncommitted changes so you can switch tasks without committing incomplete
work or losing progress.
Use Cases:
- Switching branches: Save uncommitted changes and switch branches
- Pulling Latest Changes: Stash local changes before pulling updates to avoid conflicts
- Temporary Task Switching: Pause current work, handle another task and restore changes
Examples:
- You make changes to a file and also create a new untracked file.
- eg: make changes to an index.html or any existing file
- create notes.txt (new file, not added yet)
- You realize you need to pause your work
git stash
This saves changes you made to the index.html but leaves "notes.txt" behind(It dosen't get stashed)
- You decide you want everything saved, including new file in this case the "notes.txt" and also with a
message.
git stash push -m "homepage changes" -u
Always include "-u" if you want to stash intracked files as well
- Later, you want to see what stashes exist.
git stash list
Example output:
"stash@{0}: On main: homepage changes"
- You want to apply the most recent stash
git stash apply
- You decide to apply an older stash instead.
git stash apply stash@{x}
x could be any stash number
- You want to apply the stash and remove it from the list
git stash pop
- You no longer need a certain stash.
git stash drop stash@{x}
- To remove the latest stash quickly
git stash drop
- To permanently remove all stashed changes
git stash clear