Advanced Commands: Stash and Rebase
45 min`git stash` temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stash is useful when you need to switch branches but have uncommitted changes. Stash saves your changes without creating a commit, allowing you to clean your working directory temporarily. Understanding stash enables you to manage uncommitted work effectively. Stash is perfect for quick context switches.
`git rebase` is another way to integrate changes from one branch to another, moving or combining a sequence of commits to a new base commit. Unlike merge, rebase replays your commits on top of another branch, creating a linear history. Rebase rewrites commit history, so it should only be used on local branches or branches you're sure no one else is using. Understanding rebase enables you to maintain clean project history. Rebase is powerful but requires understanding its implications.
Rebasing makes for a cleaner project history by creating a linear sequence of commits without merge commits. Linear history is easier to read and understand. Rebase eliminates unnecessary merge commits that can clutter history. However, rebase rewrites history, which can cause issues if commits have been shared. Understanding rebase trade-offs helps you decide when to use it. Rebase is great for feature branches before merging.
`git stash pop` applies the most recent stash and removes it from the stash list, restoring your stashed changes to the working directory. If the stash applies cleanly, it's removed. If there are conflicts, the stash remains for you to resolve conflicts and manually drop it. Understanding stash pop enables you to restore stashed work. Stash pop is convenient for applying and removing stashes.
`git stash list` shows all stashed changes, displaying stashes with their messages and timestamps. Stashes are stored in a stack (LIFO—last in, first out). You can apply specific stashes by index: `git stash apply stash@{2}`. Understanding stash list enables you to manage multiple stashes. Stash list helps you track what you've stashed.
Best practices include using stash for temporary work, using rebase for feature branches before merging (not on shared branches), understanding that rebase rewrites history, using `git stash apply` to keep stash after applying, and cleaning up old stashes. Understanding stash and rebase enables advanced Git workflows. These tools are powerful but require careful use. Stash and rebase are essential for efficient Git workflows.
Key Concepts
- git stash temporarily saves uncommitted changes.
- git rebase replays commits on top of another branch.
- Rebasing creates cleaner, linear project history.
- git stash pop applies and removes the most recent stash.
- git stash list shows all stashed changes.
Learning Objectives
Master
- Using git stash to temporarily save changes
- Understanding git rebase and when to use it
- Applying and managing stashes
- Using rebase to maintain clean history
Develop
- Understanding advanced Git workflows
- Managing uncommitted work effectively
- Maintaining clean project history
Tips
- Use git stash for quick context switches when you have uncommitted work.
- Use rebase on feature branches before merging for cleaner history.
- Never rebase shared branches—it rewrites history others may have.
- Use git stash list to see all your stashes.
Common Pitfalls
- Rebasing shared branches, rewriting history others depend on.
- Stashing and forgetting about it, losing track of work.
- Not understanding that rebase rewrites history.
- Using rebase when merge would be more appropriate.
Summary
- git stash temporarily saves uncommitted changes.
- git rebase replays commits for cleaner history.
- Rebase should only be used on local branches.
- Understanding stash and rebase enables advanced workflows.
- These tools are powerful but require careful use.
Exercise
Simulate stashing changes to work on an urgent fix.
# You are working on a feature, but an urgent bug appears
# Stash your changes
git stash
# Switch to main, fix the bug, and commit
# ...
# Switch back to your feature branch and re-apply your stashed changes
git switch feature-branch
git stash pop