04 March 2023
A very common statement that’s made:
Git force push is bad
This statement lacks a lot of context and paints a wrong statement.
To do a git force push use the flag — force
or -f
with the git push
command.
git push —- force
(This assumes the “upstream” is already set for the git branch)
This essentially rewrites remote history.
Hold on! Do go through the next section to understand how to effectively use this tool.
More often that not the discussion has happened how git force push is bad and looked upon. Although like any other tool, it’s about knowing what to use when to use.
My takes is as below:
Force pushing is fine as long as you know what you are doing i.e you are very well aware of the consequences - which here in case is rewriting remote history. If your context declares that is an OK decision to make then nothing to worry about.
Don’t force push to collaborative branches without consent from all collaborators.
Force pushing to private branches is fine - given you are the only person working on that branch.
Force pushing to branches that are meant to be destructive and doesn’t contribute to code history is also fine.
All in all force push is a nifty feature, it’s like saying a knife is dangerous. It’s not the knife that is dangerous it’s the intent of the person holding it is.
Rewriting remote history: What it means is that the chain of commits that are there in remote will be overwritten by the chain of commits that you have in your local machine. This is a destructive action, meaning it will make changes that are not recoverable (easily).