0

Delete remote branch commands:

  • git remote remove origin
  • git push origin --delete origin

Which is correct? Or what's the difference?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Morris Lu
  • 9
  • 2
  • 2
    Can you clarify your question a little bit? What, *precisely* do you want to do? Those two commands do completely different things, so the simple answer to "what's the difference?" is "Everything!" It's like asking "What's the difference between the Toyota Prius and the color blue?" And the answer to "Which is correct?" is, well, what do you want to do? They do completely different things, none is more correct than the other. – Jörg W Mittag May 06 '20 at 05:39
  • Sorry, my question is a question in itself. I understand now. I will figure out the knowledge and ask some high-quality questions myself – Morris Lu May 08 '20 at 02:12

3 Answers3

3

git push --delete origin <branchname>

This deletes a branch named <branchname> from the remote repository.

From your question:
git push origin --delete origin would delete the branch origin.

This is quite destructive if other users are committing and pushing to that branch. All changes on that branch will be lost. You and all others working on that branch won't be able to push/pull to/from it again.

git remote remove <name>

This removes the remote named <name> from your local copy of the repo.
This is NOT for deleting a branch.

From your question:
git remote remove origin would remove the remote origin.

The remote is where you normally cloned the repo from, and is where you push/pull branches to/from. This is not as destructive to others as it only removes your origin on your machine. But note that it can be destructive to you because "All remote-tracking branches and configuration settings for the remote are removed.".

Which is correct?

It depends on what you want to do. If you want to delete a remote branch, use git push --delete origin <branchname>. Take note that you have to specify a <branchname> and origin is normally not a branch.

See this related post on deleting branches: How do I delete a Git branch locally and remotely?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Is there a typo in your answer? Did you mean to say `This deletes a branch named ` instead of `This deletes a branch named origin` ? – Paolo May 06 '20 at 09:36
  • @UnbearableLightness I was following the command used by the OP `git push origin --delete origin` when I originally typed my answer. I updated it now to make it clearer. Thanks. – Gino Mempin May 06 '20 at 11:03
  • Thank you very much for your answers ,git's many commands, which often make me feel confused. In essence, I don't know enough about the principle. – Morris Lu May 08 '20 at 02:11
1

They are extremely different.

# lists all remotes you have added
git remote -v

# removes origin from your remotes
# doesn't delete anything in the remote repo
# you just wouldn't be able to git [fetch|pull] origin
# all local branches are kept
# just preventing yourself from reading from the remote repo and updating your local branches
git remote remove origin  

# there's never a good reason to use this
# sounds like you're trying to delete a branch called origin on the origin remote
# makes no sense
git push origin --delete origin

Probably look into "git [branch|remote] --help"

Jeremi G
  • 405
  • 2
  • 8
0

The git remote command is mostly for manipulating "remotes".

That's kind of circular:

  • Q: What's git remote?
  • A: A command to manipulates remotes.
  • Q: What's a remote?
  • A: The thing git remote manipulates.

But in fact, a remote is a very specific thing in Git. It's a name, and under that name, Git keeps the URL of some other Git.

There is a bit more to it than that, but it's enough to get started: just remember, in Git, a remote is a name for the URL by which your Git calls up another Git. In effect, it's like a contact in your address-book in your phone. You don't have to remember Joe's phone number, you can just remember Joe's name.

Hence:

git remote delete origin

tells your Git: Forget the name origin, and with it, the URL my Git uses to call up some other Git.

On the other hand, git push has your GIt call up another Git. The syntax for this is git push remote-name stuff-to-do. So git push origin means: Hey, my Git! Call up another Git! Look up their "phone number" using the name origin!

Once your Git calls up their Git, the two Gits will have a conversation. Your Git will send them stuff (if you ask your Git to do that), and then your Git will ask them to do things to their branches and/or tags. With:

git push origin --delete <branch-name>

you are telling your Git: Call the Git whose number is in the address book under the name origin, and ask them to delete the branch I named here. So if your goal is to have them delete a branch named origin, the command you want would be git push origin --delete origin.

torek
  • 448,244
  • 59
  • 642
  • 775