When You Need a Force Push
A normal push only adds commits on top of the remote branch. After you rewrite commits that are already pushed, your branch and the remote branch have different histories, and Git rejects the normal push. Typical causes:
- A rebase of a pushed feature branch onto a newer
main. See Git merge vs rebase. - An amend of a pushed commit, to change its message or content. See Git commit --amend.
- A reset that removed pushed commits, or a squash of several commits into one.
If the rejection came because someone else pushed, you do not need force at all: fetch, integrate their work, and push normally, as shown in git push: upload commits to a remote.
Why It Is Dangerous
Force makes the remote branch point where your local branch points. Any commit on the remote that your local branch does not contain disappears from that branch, including commits a teammate pushed a minute ago. They are still in that teammate's clone, but the shared branch no longer has them, and the next person to reset to it loses them too.
Use --force-with-lease
Warning: the commands in this section overwrite the remote branch. Use them only on a branch you own or have agreed on with your team.
git push --force-with-lease origin feature/login
With a lease, Git compares the remote branch with your remote-tracking branch origin/feature/login, which records where the remote was at your last fetch. If the remote has moved since, someone pushed, and Git refuses. Fetch, look at their commits, integrate them, and try again.
One gap remains: if something fetches in the background, for example an editor, your remote-tracking branch is updated without you looking at the new commits, and the lease passes.
Add --force-if-includes
git push --force-with-lease --force-if-includes origin feature/login
This extra check closes the gap. Git also verifies that the current tip of the remote-tracking branch has been part of your local branch at some point, according to your reflog, which means you actually integrated it. A background fetch alone no longer lets the push through. The option arrived in Git 2.30 and does nothing without --force-with-lease.
To make the safe form short to type:
git config --global alias.pushf "push --force-with-lease --force-if-includes"
Plain --force
Warning: the following command overwrites the remote branch without any check and can delete other people's commits.
git push --force origin feature/login
-f is the short form. Reserve it for a branch nobody else uses, and even then the lease form costs nothing.
Never on a Shared Main Branch
Do not force push main or any branch others build on. To undo work there, add a commit that reverses it, as in Git revert: undo a commit safely. Most Git hosting platforms let you mark branches as protected, which rejects force pushes to them; turn that on for main and release branches.
After a Force Push
Teammates who have the old version of the branch should not merge it with the new one. If they have no local work on it, they can move their branch to the new remote state. Warning: this discards their uncommitted changes and local commits on that branch:
git fetch origin
git reset --hard origin/feature/login
If they do have local commits, git rebase origin/feature/login replays them on top instead.
Recover Overwritten Commits
The lost commits still exist in every clone that fetched them before the force push. In such a clone, find the old tip with git reflog or with git reflog show origin/feature/login, then save it as a branch and push it:
git branch rescue f6e5d4c
git push -u origin rescue
Merge or cherry-pick from rescue back into the branch. Act soon, because old reflog entries expire.