Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Critical situation in removing a local branch

I'm using the following command to remove a local branch with force delete option:

$ git branch -D <branch_name>

My question is, If I delete a local branch that had an upstream set and then do a normal push, it won't delete the remote branch right?

What should I do in this situation?


[NOTE]:

  • "-D" is force delete option.
  • I want to delete the local branch and keep the remote branch on origin.

like image 691
Benyamin Jafari Avatar asked Jul 11 '18 23:07

Benyamin Jafari


1 Answers

git will only delete your local branch, please keep in mind that local and remote branches actually have nothing to do with each other. They are completely separate objects in Git.

Even if you've established a tracking connection (which you should for most scenarios), this still does not mean that deleting one would delete the other, too!

If you want any branch item to be deleted, you need to delete it explicitly.

Deleting local branches in Git

git branch -d <branch_name>

using a capital -D is like a "force" version of -d. If the branch isn't fully merged you'll get an error if you use the lowercase version. This again has no relevance to the remote branches and will only delete your local branch.

Deleting remote branches in Git

git push origin --delete <branch_name>

so to your question

If I delete a local branch that had an upstream set and then do a normal push, it won't delete the remote branch right?

You are correct it will not delete the remote branch.

like image 62
Josh Stevens Avatar answered Sep 19 '22 14:09

Josh Stevens