When I found my wrong mistakes at remote branch, I always delete this, squash locally and push again. Because I heard 'git push -f' was dangerous. However I noticed push result showing after git fetch is some thing like below.
new BRANCH_NAME "(force)"
My workflow deleting and pushing again is correct? What is different between the way and 'push -f'?
What happens if I push the way it is? If you git push , it will simply re-create the old branch name on the remote. If you hadn't renamed your branch this would likely be fine assuming you intended to re-use the same branch name, but since you renamed your branch, this is undesirable.
Pulling never deletes your local branch. If your other developer uses git fetch --prune , his local remote tracking branches (e.g. remote/origin/name_of_branch ) would be deleted, but the local version he is working on should stay untouched, and when he pushes them again, the branch would be recreated.
Branches can be safely removed without risk of losing any changes. Consider a scenario in which a branch patch-1 is about to be merged with the master branch through a pull request. Before the merge, master and patch-1 both point to separate commits in git's commit history.
Pushing to delete remote branches also removes remote-tracking branches. Note that deleting the remote branch X from the command line using a git push will also remove the local remote-tracking branch origin/X , so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p .
Functionally, deleting and pushing again is at least as dangerous as git push -f
.
However, deleting and pushing again is actually worse than git -f push
for a very specific reason:
At least with git -f push
, the replacement is atomic.
Albeit, this isn't a big deal when you're working solo.
git -f push
, you're told exactly what replacement took
place in the resulting message: X...Y branch ->
branch (forced update)
. From there, you can make sure X
is really what were meaning to blow away.git push origin :branch && git push origin branch
, you can't really tell what you deleted if you aren't absolutely the only person possibly pushing to branch
.No, deleting the remote branch first is not going to make it safer or anything.
The reason forced push is dangerous is due to workflow reason, not technical reasons. If someone pulled your erroneous branch, then you force push the correct change set on the branch, that may cause havoc on their history and on any changes they made on top of the branch because their pulls will no longer be fast forward (unless they fix their erroneous branch with force pull or if they rebase their changes), which could mean that they might reintroduce the erroneous changes again. In a team with just one committers, this usually won't be much of an issue, with larger projects, with lots of committers and lots of watchers, then the likelihood of problem much larger because it may not be just one or two person who've downloaded the erroneous branch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With