TIL: Git push force with lease

Hi there,

I'm used to rebase my PRs with a git rebase and force push, I discovered my new friend: git push --force-with-lease thanks to my coworkers.

Git rebase?

Git rebase is helpful to rewrite history of the commits in a specific branch. You have the full documentation of git push. If you are new or not fully familiar to git, I quite like this website in order to to learn git and practice at the same time. You can rewrite the history of your commits and then force push.

--force vs --force-with-lease

The difference between --force and --force-with-lease is with --force-with-lease will push only if you are not overwriting any work on the remote branch if more commits were added to the remote branch. If is not the case, the push will be cancelled and you will get an error.

--force will overwrite any work in the remote branch, no matter if there are anything new in the branch. As you can see, --force-with-lease is a safer option if you don't want to overwrite someone elses work by force pushing.

Bonus: --force-if-includes

I discovered today via Mastodon that Adam Johnson has done a post related to this before this one, worth take a look, I also learned about --force-if-includes: https://adamj.eu/tech/2023/10/31/git-force-push-safely/

That’s it for now! I hope it will be useful for someone.