Sub topic 5
Your default: git push --force-with-lease
Prerequisites
This chapter assumes you can commit, branch, push, and pull, and that you have used git rebase or git commit --amend. It also assumes you know origin is the usual name of a remote. Earlier chapters explained why a rewritten branch is rejected as a non-fast-forward and what plain --force can destroy. Here you learn the command to use instead.
The idea
After a rebase or amend, your local feature/login no longer extends the commits on the remote. A normal push is rejected. Plain git push --force fixes that by overwriting the remote branch with whatever you have, even if a teammate pushed to it an hour ago.
--force-with-lease adds a condition to the overwrite. It means: replace the remote branch only if it still points where my remote-tracking branch says it points. Your remote-tracking branch, origin/feature/login, records where the remote branch was at your last fetch or push. If the remote has moved since then, Git refuses and your teammate's work survives.
A worked example
You rebase feature/login onto the latest main, then push:
git switch feature/login
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature/loginWalk through what happens:
git fetch originupdatesorigin/mainandorigin/feature/login.git rebase origin/mainrewrites your local commits on top of the newestmain.git push --force-with-leasecompares the real remotefeature/loginwith yourorigin/feature/login.- If they match, Git overwrites the remote branch.
A successful push prints a line with a leading + and the words "forced update":
To github.com:example/repo.git
+ 3f2a1bc...9d8e7f6 feature/login -> feature/login (forced update)The + marks a non-fast-forward update. The two hashes are the old and new tips of the remote branch.
Comparing the three pushes
| Command | Rewritten history | Remote has new commits you have not seen |
|---|---|---|
| `git push` | Rejected | Rejected |
| `git push --force` | Overwrites | Overwrites, and those commits are lost from the branch |
| `git push --force-with-lease` | Overwrites | Rejected |
Commits dropped from a branch are not deleted at once. A teammate may still have them locally, and the remote may keep them unreferenced for a while. Recovery is awkward, though, which is why you want the rejection instead.
Always name the remote and branch
Write the remote and the branch every time:
git push --force-with-lease origin feature/loginWithout them, Git falls back to your push.default setting and any configured upstream. You can end up checking and overwriting a different branch than you meant to. Naming both keeps the lease aimed at a single branch.
Common errors at this stage
- Using
--forceout of habit. It runs fine and gives no warning, so the mistake only shows up when a teammate's commit disappears. Prefer the lease form every time. - Pushing without fetching first. If you never fetched,
origin/feature/loginmay be older than the real remote branch. The lease then fails with a "stale info" rejection even though you did nothing wrong. The next chapter covers how to read that message and recover. - Assuming the lease is a guarantee. It compares against your remote-tracking branch. If something refreshes that branch without you reviewing the new commits, the check can pass when it should not. A later chapter shows how this happens and how to close the gap.
Exercises
Exercise
You rebased feature/login and run git push --force-with-lease origin feature/login. A teammate pushed one new commit to that branch ten minutes ago, and you have not fetched since. What happens, and why?
Show answer
The push is rejected. The remote branch has moved to a commit your origin/feature/login does not record, so the lease condition fails. Plain --force would have overwritten the teammate's commit.
Exercise
Why is it better to write git push --force-with-lease origin feature/login than just git push --force-with-lease?
Show answer
Naming the remote and branch fixes exactly which branch is checked and overwritten. Without them, Git uses push.default and upstream settings, which can select a different branch than you intended.