Pushing after Rewriting History

If we've previously pushed the branch, the server will reject our push after rewriting history, which can be forced with --force-with-lease.

Important:

Before using the commandline version of force push such as git push --force-with-lease or git push --force, you should make sure the push default setting is not match. When it's match git will push all your local branches that matches the branches on the remote server thus extremely dangerous when combined with force

# to get the value from the global settings
# assuming it.s global as very likely you won't configure it per project
git config --global --get push.default
# to set the value to nothing
git config --global push.default nothing

See https://git-scm.com/docs/git-config#git-config-pushdefault for other options

What is the difference between force-with-lease and force push

Force with lease is a little bit safer than force as it checks if the remote branch at local PC is the same as the branch at the remote server, for example, the push of awesome_branch to GitHub (let's say we are using GitHub) will fail if the local origin/awesome_branch at our side is different to the awesome_branch on GitHub (Someone else may have pushed something to the branch, but we haven't fetch the commits).

Last updated