How to Force Push in Git
Force pushing in Git overwrites the remote repository history with your local changes, which can be necessary after operations like rebasing or amending commits. As the creator of CoreUI with over 25 years of software development experience, I use force push carefully when cleaning up commit history before merging feature branches. The safer approach is using --force-with-lease instead of --force to prevent accidentally overwriting other developers’ work.
Use git push --force-with-lease to safely force push changes while protecting against overwriting other developers’ commits.
# Safer force push (recommended)
git push --force-with-lease origin feature-branch
# Traditional force push (more dangerous)
git push --force origin feature-branch
# Force push to specific branch
git push --force-with-lease origin main
# Force push all branches (very dangerous)
git push --force-with-lease origin --all
# Check what would be pushed before forcing
git push --dry-run --force-with-lease origin feature-branch
The --force-with-lease option checks that the remote branch hasn’t been updated by someone else since your last fetch. If it has been updated, the push is rejected, preventing you from accidentally overwriting others’ work. Use --force only when you’re certain no one else has pushed to the branch. Force pushing is typically needed after interactive rebasing, commit amending, or history rewriting operations. Always communicate with your team before force pushing to shared branches.
Best Practice Note:
In CoreUI development, we use --force-with-lease exclusively when cleaning up feature branches before merging to main. This approach maintains clean commit history while protecting against accidental data loss, which is essential for maintaining code quality and team collaboration in our open-source projects.



