Git Push --force
Learn about force pushing and when to use it safely
Git Push --force
Force pushing overwrites the remote branch with your local changes. Use with extreme caution as it can overwrite other people's work.
Basic Commands
# Force push (dangerous)
git push --force origin branch-name
# Safer force push
git push --force-with-lease origin branch-name
When to Use Force Push
After History Rewriting
# After amending commits
git commit --amend
git push --force-with-lease origin feature-branch
# After interactive rebase
git rebase -i HEAD~3
git push --force-with-lease origin feature-branch
Fixing Mistakes
# After squashing commits
git rebase -i HEAD~5
git push --force-with-lease origin feature-branch
Safe Force Push
Use --force-with-lease
# Safer option - checks if remote has changed
git push --force-with-lease origin feature-branch
Best Practices
- Only force push on feature branches you own
- Never force push to main/master
- Communicate with team before force pushing shared branches
- Use
--force-with-lease
instead of--force
Common Scenarios
1. Amended Commit
git commit --amend -m "Better commit message"
git push --force-with-lease origin feature-branch
2. Cleaned Up History
git rebase -i HEAD~3
git push --force-with-lease origin feature-branch
3. Reset to Previous Commit
git reset --hard HEAD~1
git push --force-with-lease origin feature-branch
Danger Zones
Never Do This
# ❌ Force push to main branch
git push --force origin main
# ❌ Force push without communication
git push --force origin shared-branch
Recovery
If You Force Pushed by Mistake
# Check reflog on remote
git reflog origin/branch-name
# Reset to previous state
git reset --hard origin/branch-name@{1}