Git Undo
In the process of using Git, you will inevitably encounter situations where you need to undo operations. This chapter details various undo and restore operations to help you safely handle "regret" scenarios.
Basic Concepts of Undo
Git's Three Areas
Before understanding undo operations, you need to be clear about Git's three main areas:
Working Directory
↓ git add
Staging Area
↓ git commit
RepositoryUndo methods and scopes differ across these areas.
Safety of Undo Operations
- 🟢 Safe: No data loss, recoverable.
- 🟡 Caution: May lose uncommitted changes.
- 🔴 Dangerous: Will permanently lose data, proceed with extreme caution.
Undo in Working Directory
Undo File Changes
bash
# Undo changes to a single file 🟢
git checkout -- filename.txt
# Undo changes to multiple files 🟢
git checkout -- file1.txt file2.txt
# Undo changes to all files 🟡
git checkout -- .
# Using new syntax (Git 2.23+) 🟢
git restore filename.txt
git restore .Demo: Undo File Changes
bash
# Create demo environment
mkdir git-undo-demo
cd git-undo-demo
git init
# Create initial file
echo "Original content" > file.txt
git add file.txt
git commit -m "Initial commit"
# Modify file
echo "Modified content" > file.txt
echo "New content" >> file.txt
# Check changes
git diff
# Undo changes
git checkout -- file.txt
# Verify result
cat file.txt # Should show "Original content"Delete Untracked Files
bash
# Check untracked files
git clean -n
# Delete untracked files 🟡
git clean -f
# Delete untracked files and directories 🟡
git clean -fd
# Interactive delete 🟢
git clean -i
# Delete ignored files 🟡
git clean -fX
# Delete all untracked files (including ignored) 🔴
git clean -fxUndo in Staging Area
Unstage Files
bash
# Unstage single file 🟢
git reset HEAD filename.txt
# Unstage all files 🟢
git reset HEAD
# Using new syntax (Git 2.23+) 🟢
git restore --staged filename.txt
git restore --staged .Demo: Unstage
bash
# Modify file and add to staging
echo "New modification" >> file.txt
git add file.txt
# Check status
git status
# Unstage
git reset HEAD file.txt
# Check status (File modified but not staged)
git statusPartial Unstage
bash
# Interactive partial unstage
git reset -p
# Or use restore
git restore --staged -p filename.txtUndo Commits
Modify Last Commit
bash
# Modify last commit message 🟢
git commit --amend
# Modify message without opening editor 🟢
git commit --amend -m "New commit message"
# Add file to last commit 🟢
git add forgotten_file.txt
git commit --amend --no-editDemo: Modify Commit
bash
# Create commit
echo "Feature A" > feature_a.txt
git add feature_a.txt
git commit -m "Add Feature A"
# Realized missed file
echo "Feature A Config" > feature_a_config.txt
git add feature_a_config.txt
# Modify last commit
git commit --amend -m "Add Feature A and Config"
# Check history
git log --onelineUndo Commit but Keep Changes
bash
# Undo last commit, keep changes in Working Directory 🟢
git reset --soft HEAD~1
# Undo last commit, keep changes in Staging Area 🟢
git reset --mixed HEAD~1
# Or simply
git reset HEAD~1
# Undo last commit, discard all changes 🔴
git reset --hard HEAD~1Undo Multiple Commits
bash
# Undo last 3 commits 🟡
git reset --soft HEAD~3
# Undo to specific commit 🟡
git reset --soft commit_hash
# View revocable commits
git log --oneline -10