Something I’ve seen “hammered in” repeatedly is that one should not ever git push --force, though surely there are times when it is appropriate?
When is –force intended to be used?
My current example:
- Create branch and make some changes.
- Push branch “live” to show code.
- Rebase on origin, make changes/re-write history in the process.
- My code has now diverged from my own remote code.
Is push –force the appropriate solution to this?
Rewriting history will be a headache for anyone who pulled your repo and made their own changes based on your branches. When you rewrite your remote history and the downstream people pull it, their changes will end up dangling off in the middle of nowhere. (Check out the Recovering from an upstream rebase section of the
git rebasedocs.)That’s why if other people are using your repo, it would generally be better to merge changes from origin into
liveand push the merge. It’s not as clean as a rebase, but no one downstream will need to do funky things to keep up with your rewrites:You can always rebase when you’re ready to pull the changes in
liveback into the main branch.On the other hand, if you’re the only one using your repo, or the people downstream are ok with fixing remote history rewrites,
--forceaway.