Sub topic 4
What plain --force can destroy
This chapter assumes you know how to commit, branch, push and pull, and that you have used git rebase or git commit --amend. It also assumes you know what a remote such as origin is. The earlier chapters explained why a rebase makes a normal push fail with a non-fast-forward rejection. Here you will see what git push --force does to get past that rejection, and what it can destroy along the way.
What --force actually does
A normal push asks the remote to move a branch from its current commit to your commit. The remote agrees only if your commit descends from the current one (a fast-forward). --force removes that condition. The remote moves the branch to your commit whatever it currently points to.
Git does not compare anything before it overwrites. It does not check whether the remote branch matches your last fetch, and it does not check whether anyone else pushed. Whatever the branch held is replaced.
git switch feature/login
git rebase origin/main
git push --force origin feature/loginGit's output marks the overwrite with a + and the words "forced update":
To github.com:example/repo.git
+ 4d2e9a1...b81c6f3 feature/login -> feature/login (forced update)The three-dot range 4d2e9a1...b81c6f3 shows the old and new tips. Git prints that line whether or not the old tip held work you knew about.
A concrete loss
Suppose Alice and Bob both work on feature/login.
- Alice fetches, so her
origin/feature/loginpoints to commit B. - Bob pushes commit C on top of B. The remote branch now points to C.
- Alice rebases her branch onto the latest
main. This rewrites B into B', and her local branch now ends at B'. - Alice runs
git push --force. The remote branch moves from C to B'.
Alice's push succeeded, and nothing told her that Bob's commit was gone. Her terminal shows the same "forced update" line as in a safe push. Bob's commit is not deleted from the repository immediately, but no branch points to it, so it is unreachable from the branch. Servers remove unreachable objects during garbage collection, on a schedule you do not control.
Why the damage is quiet
Three things make this failure hard to notice:
- Git gives no warning.
--forceis an explicit instruction to skip the safety check, so Git skips it. - The loss is on the remote. Alice's own history looks correct. Only someone who compares the remote branch against what they expected would see the difference.
- Bob finds out late. His next
git pullmay report conflicts, or a rebase may replay commits that were already rewritten. Both look like ordinary merge problems, not a lost push.
The risk grows with the number of people who push to the branch, and with how long it has been since you last fetched. A solo feature branch that only you push to carries almost no risk. A shared integration branch carries a lot.
Recovering lost commits
If a force push removed your commit, a copy usually still exists somewhere. Look in the clone of whoever made the commit. Bob can find it in his reflog, which records where his refs have pointed:
git reflog show feature/login
git branch rescue/login <sha-of-commit-C>Replace <sha-of-commit-C> with the hash from the reflog. The new branch rescue/login keeps commit C reachable, and Bob can then rebase it onto the new history or cherry-pick it. Reflog entries expire after a while, 90 days by default for reachable entries and 30 days for unreachable ones, so recovery works best when you act quickly. If nobody has a local copy, you depend on server-side tools or support, and many hosts do not offer them.
Recovery is a repair, and it takes coordination between Alice and Bob. The next chapter shows how to avoid needing it.
Summary of the risk
| Question | Plain `--force` |
|---|---|
| Does it compare the remote branch to anything? | No |
| Does it warn if someone else pushed? | No |
| What happens to commits only on the remote? | They become unreachable from the branch |
| Where can they be recovered from? | Usually a teammate's local clone or reflog |
Exercise
Alice fetched at 10:00, so her origin/feature/login points to commit B. At 10:05 Bob pushes commit C on top of B. At 10:10 Alice rebases her branch and runs git push --force origin feature/login. What happens to commit C, and why does Alice not see an error?
Show answer
The remote branch moves from C to Alice's rebased tip, so C is no longer reachable from the branch. Alice sees no error because plain --force does not compare the remote branch with her last-known state. It overwrites the branch and prints the normal "forced update" line.
Exercise
Bob's push of commit C was overwritten, but he still has his local feature/login branch. Which commands recover C onto a new branch, and where do you find the commit hash?
Show answer
Run git reflog show feature/login to find the hash of C, then git branch rescue/login <hash>. The reflog in Bob's own clone records where his local branch has pointed.