Skip to content

How to safely push rebased Git branches

During a pair programming session today, my excellent colleague Rafael J. introduced me to a safer way to push a rebased Git branch to a repository.

Normally, rebasing a working branch from trunk rewrites this branch’s history. But because there’s a risk of rewrites affecting other people’s work, Git prevents you from pushing them to the repository — unless you use the well-known --force option.

Unfortunately, --force is destructive and will overwrite any subsequent changes made by other people on that branch, obliterating their hard work.

The option --force-with-lease (documentation) exists to address this problem, and will abort the push if your local copy of the branch is stale and missing changes not made by you.

> git push --force-with-lease

It’s not a foolproof method, but still an improvement over what I had been doing before.