Git 4b 🧩 Stashing Changes
4b Stashing Changes
Git stashing is a useful feature that allows you to temporarily save your uncommitted changes without making a commit. This is particularly helpful when you need to switch between different tasks or branches but don’t want to commit incomplete work.
Basic Stash Commands
- git stash
This command saves your current uncommitted changes (both staged and unstaged) and reverts your working directory to match the last commit.
git stash
- git stash list
This shows all the stashed changes in your repository, displaying them in chronological order with descriptions.
git stash list
- git stash pop
This applies the most recent stash and removes it from the stash list.
git stash pop
- git stash apply
This applies a specific stash without removing it from the stash list, allowing you to apply the same stash multiple times.
git stash apply [stash-name]
- git stash drop
This deletes a specific stash from your stash list.
git stash drop [stash-name]
Practical Examples
Let’s walk through some common scenarios where stashing is useful:
Example 1: Switching Branches Temporarily
You’re working on feature-branch but need to check something on main
$ git status
On branch feature-branch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: src/login.js
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: src/dashboard.js
$ git stash
Saved working directory and index state WIP on feature-branch: abc1234 Add login functionality
$ git checkout main
Switched to branch 'main'
$ git checkout -b hotfix/urgent-bug
Switched to a new branch 'hotfix/urgent-bug'
After fixing the urgent bug, switch back to feature-branch
$ git checkout feature-branch
Switched to branch 'feature-branch'
$ git stash pop
On branch feature-branch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: src/login.js
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: src/dashboard.js
Example 2: Managing Multiple Tasks
You’re working on a feature but need to test something quickly
$ git stash
Saved working directory and index state WIP on develop: def5678 Implement user profile
Make quick changes for testing
$ git add test.js
$ git commit -m "Quick test changes"
$ git push origin develop
Switch back to original work
$ git stash pop
On branch develop
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: src/user-profile.js
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: src/dashboard.js
Example 3: Working with Multiple Stashes
Create first stash
$ git stash save "Working on login form"
Saved working directory and index state WIP on develop: def5678 Implement user profile
Make more changes and create second stash
$ git stash save "Fixing dashboard layout issues"
Saved working directory and index state WIP on develop: def5678 Implement user profile
View all stashes
$ git stash list
stash@{0}: On develop: Fixing dashboard layout issues
stash@{1}: On develop: Working on login form
Apply specific stash$ git stash apply stash@{1}
Drop a specific stash
$ git stash drop stash@{0}
Dropped stash@{0} (34a2b5c...)
Advanced Stash Commands
- git stash save [message]
You can add a descriptive message to your stash:
git stash save "Fixing authentication bug"
- git stash show
This shows the changes in the most recent stash:
git stash show
- git stash show -p
This shows the full patch of the most recent stash:
git stash show -p
- git stash branch [branch-name]
This creates a new branch from a stash and applies it:
git stash branch new-feature-stash
Stash with Conflicts
When applying stashes, conflicts may occur if the files have been modified in both the stash and the current working directory:
$ git stash pop
Auto-merging src/dashboard.js
CONFLICT (content): Merge conflict in src/dashboard.js
The stash change could not be applied.
In this case, you’ll need to resolve the conflicts manually, then stage and commit the resolved files.
Can you answer these Questions
- What is the main purpose of git stash and when would you use it?
- How does git stash pop differ from git stash apply in terms of stash management?
- What happens to your stashed changes if you run git stash pop multiple times?
- Can you have multiple stashes at the same time? How do you manage them?
- What is the difference between git stash save and just git stash?
- Why might you want to use git stash branch instead of git stash apply?
- What are the potential risks of using stashing in a collaborative environment?
- How can you see what changes are included in a specific stash without applying it?
- What happens to your staged and unstaged changes when you run git stash?
- Can you apply a stash to a different branch than where it was created?
- What should you do if you encounter merge conflicts while using git stash pop?
- How can you see the full patch content of your stashed changes?
- What is the benefit of adding descriptive messages when saving stashes?
- What happens to a stash when you successfully apply it with git stash pop?
- Why would you use git stash show -p instead of just git stash show?