---
title: Force push with care
teaser: A gentler `git push --force`.
tags: git,tools
author: Tute Costa
published_on: 2016-03-10
---

![How git push force feels like](https://images.thoughtbot.com/git-push-force-with-lease/XEDULrg2QsnzIGytOMfh_XFQLB.jpg)

My coworker [Calle] \(not in the picture) suggested in our guides that we
[prefer the flag `--force-with-lease` over `--force`][pr] to the `git push`
command. This option allows one to force push without the risk of
unintentionally overwriting someone else’s work. It will update remote
references only if it has the same value as the remote-tracking branch we have
locally. In other words, only if nobody has updated the branch upstream. If
there are new remote commits, `--force-with-lease` will fail with a “stale info”
message, prompting us to fetch first.

[Calle]: https://twitter.com/calleerlandsson
[pr]: https://github.com/thoughtbot/guides/pull/363

`--force-with-lease` fits nicely into our workflow of [rebasing and squashing
commits] before merging into main, and we use it so often that we [created an
alias in our `gitconfig`][alias]:

```gitconfig
[alias]
    pf = push --force-with-lease
```

[rebasing and squashing commits]: https://thoughtbot.com/blog/git-interactive-rebase-squash-amend-rewriting-history
[alias]: https://github.com/thoughtbot/dotfiles/pull/431

I now always push with the `pf` alias, which always gets my work merged with
upstream. Unless it could set the house on fire.
