Handling Uncommitted Work
Understanding Uncommitted Changes
When you have modifications that haven't been committed, Git provides several strategies to manage these changes before switching branches or performing other operations.
Change States in Git
State |
Description |
Action Needed |
Staged |
Changes added to staging area |
Ready to commit |
Unstaged |
Modified but not added |
Requires git add |
Untracked |
New files not in repository |
Requires tracking |
Stashing Uncommitted Work
## Stash current changes
git stash
## Stash with a descriptive message
git stash save "Work in progress: login feature"
## List all stashes
git stash list
## Apply the most recent stash
git stash apply
## Apply and remove the stash
git stash pop
## Clear all stashes
git stash clear
Stash Workflow Visualization
stateDiagram-v2
[*] --> Working
Working --> Stashed : git stash
Stashed --> Working : git stash pop
Working --> Committed : git commit
Handling Uncommitted Changes Before Branch Switch
## Option 1: Commit changes
git add .
git commit -m "Checkpoint commit"
## Option 2: Stash changes
git stash
## Option 3: Discard changes (careful!)
git reset --hard HEAD
Advanced Stash Techniques
## Create a branch from a stash
git stash branch new-branch-name
## Show stash contents without applying
git stash show -p
Best Practices
- Always stash or commit before switching branches
- Use descriptive stash messages
- Regularly clean up stashes
- Avoid losing work by using stash or commit
With LabEx, you can practice these Git stashing techniques in a safe, interactive environment.