I want to add another comment about git push -f
.
Even when you do want to force push, 99.5% of the time git push -f
is the wrong choice.
Instead, you should be using git push --force-with-lease
almost every time.
-f
means "always push, even if someone else has pushed commits to that branch". --force-with-lease
means (more or less) "force push only if you were the last person to touch that branch". The only reason to use -f
over --force-with-lease
is if you are deliberately overwriting someone else’s work.
https://stackoverflow.com/questions/52823692/git-push-force-with-lease-vs-force has more details.
Now, I personally force push many times every day.
But that's because I do all my work on private branches that no one else reads, and then use rebase and force-pushing to create near-perfect commits, and those are what other people see.
Even so, I use --force-with-lease
, just out of habit, because I have an alias gpf='git push --force-with-lease'
.
I never force push onto public branches, unless there's some terrible emergency. (See my impending comment on Robert Jakob's comment.)
Thanks for a fun article!