Mastering Git Stash Command: Your Ultimate Guide for Efficient Code Management!
This video explains how to handle uncommitted local changes in Git when switching branches, focusing on the use of the Git stash command to temporarily save changes without committing them. It covers practical scenarios, commands, and nuanced options for effective stash management, including handling tracked/untracked files and applying or deleting stashes.
1. Problem: Switching Branches with Uncommitted Changes
Switching branches in Git while having uncommitted local modifications can cause conflicts or errors. The core problem is wanting to switch branches without carrying local changes or losing them unnecessarily.
- When switching branches with local changes, errors or merge conflicts may occur.
- Sometimes, developers want to switch branches but preserve local uncommitted changes temporarily.
- Git does not allow direct switching if it leads to conflicts between current changes and the target branch’s content.
2. Solution: Using Git Stash to Save Local Changes Temporarily
The recommended way to handle this problem is to stash the local changes using git stash, which saves the working directory and index changes temporarily in memory, allowing branch switching without errors.
git stashsaves the working directory state to the latest commit reference and clears the working tree.- After stashing, the working directory becomes clean, enabling safe branch switching.
- Once in the target branch, you can reapply the stashed changes using
git stash applyorgit stash pop. git stash applyapplies the changes but keeps the stash on the stack, allowing repeated use.git stash popapplies the changes and removes that stash from the stack.
3. Key Git Stash Commands and Their Usage
git stashautomatically stashes tracked and staged files but does not include untracked files unless specified.- To stash untracked files (new files Git doesn't track yet), use
git stash --include-untrackedorgit stash -u.
4. Managing Multiple Stash Entries
Multiple stash entries can be saved and managed like a stack:
- Multiple stashes can be stored sequentially.
git stash listshows these with labels likestash@{0},stash@{1}, etc.- Target a specific stash by its identifier when applying, dropping, or inspecting.
- You can selectively apply a stash with
git stash apply stash@{n}without removing it. - Stashes can be selectively deleted by
git stash drop stash@{n}. - All stashes can be cleared at once with
git stash clear.
5. Handling Untracked Files in Stash
Untracked files (files not yet added to the index) are not included in the default stash:
- When stashing normally, untracked files remain unchanged and appear in
git status. - To stash untracked files, use:
git stash --include-untrackedorgit stash -u. - This command stashes both tracked and untracked files, enabling cleaner branch switching without residual untracked files.
- Use
git stash applyorgit stash popto restore these changes, including untracked ones.
6. Practical Workflow Summary
Typical workflow using stash for branch switching:
- Make local changes in your current branch.
- Run
git stashto save the changes and clean the working directory. - Switch to another branch with
git switch <branch>orgit checkout <branch>. - Continue work or inspect the branch without interference from previous changes.
- When ready, switch back to the original branch.
- Recover the stashed changes with
git stash applyorgit stash pop. - Optionally, manage stash entries with
git stash list,git stash drop, andgit stash clear.
Handling untracked files:
- Always use
git stash --include-untrackedif you want to stash new files not yet added. - This prevents clutter from untracked files when switching branches, ensuring a truly clean working tree.
7. Important Tips and Considerations
- Stash operations are safe and reversible; changes are not lost unless stash is explicitly dropped or cleared.
- Avoid confusion by naming stashes with messages when saving multiple stashes.
- Inspect stashes with
git stash show -p <stash>to see explicit diffs. - The stash stack behaves like a LIFO (Last In, First Out) structure but does not require popping to apply older entries.
- Stashing files being tracked vs. untracked requires awareness to avoid losing untracked files accidentally.
Summary Table of Git Stash States and Commands
In conclusion, Git stash is a crucial command for safely managing local changes during branch switching, especially when uncommitted changes would otherwise block the operation or cause conflicts. Understanding the use of stash, including handling untracked files, stack management, and selective application, empowers developers to maintain clean workflows and avoid errors during branch manipulation.