Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'git push -f' and 'deleting remote branch and push again'

Tags:

git

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'?

like image 420
Sea Mountain Avatar asked Aug 11 '13 07:08

Sea Mountain


People also ask

What happens if you push to a deleted branch?

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.

What happens when you delete a remote branch?

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.

Is it safe to delete remote branch?

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.

Does deleting remote branch delete local branch?

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 .


2 Answers

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.

  1. When you use 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.
  2. When you do 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.
like image 119
antak Avatar answered Oct 12 '22 00:10

antak


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.

like image 38
Lie Ryan Avatar answered Oct 12 '22 01:10

Lie Ryan