Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete git branch that have same name as tag?

Tags:

git

In my project I've deleted all old branches that was hanging in git, but some branches I was not be able to remove:

$ git push origin :0.2.0.0
error: dst refspec 0.2.0.0 matches more than one.
error: failed to push some refs to 'ssh://git@<repo>.git'

I have few git repos and I have some old branches that was probably created by mistake. Those branches have the same name as a tag that was created and I'm not able to delete those branches.

Is there a way to delete those branches? It seems that the only command that you can find anywhere is git push origin :<branch> or git push -d origin <branch> and there are not distinction between branch and tag in those commands.

I have no idea how those branches were created, but I would like to get rid of them.

like image 388
jcubic Avatar asked Apr 19 '21 10:04

jcubic


People also ask

Can a tag have the same name as a branch git?

You should never name a tag and a branch the same name! It makes sense in a case like this to use naming conventions on the tags to keep from colliding on the branch names. Versus when we releasing code from the master branch. You can find the common parent in git using merge-base to do a Tag on code from the past.

How do I remove a specific branch in git?

The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.

How do I delete a branch name?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

How to delete a remote branch in Git?

The command to delete a remote branch is: Instead of using the git branch command that you use for local branches, you can delete a remote branche with the git push command. Then you specify the name of the remote, which in most cases is origin.

When should I use the delete command in Git?

Use it only when you are absolutely sure you want to delete a local branch. If you didn't merge it into another local branch or push it to a remote branch in the codebase, you will risk losing any changes you've made. Remote branches are separate from local branches. They are repositories hosted on a remote server that can be accessed there.

How do I push a renamed git branch to another git branch?

type git branch to get the list of all branches. if the renamed branch is not pushed to the remote then you can push it normally by if the renamed branch is already there on the remote ( if you push it using the above method, it will create a new branch instead of replacing it) use

How to delete a remote Git tag using Git push?

Back to the previous example, if you want to delete the remote Git tag named “v1.0”, you would run To delete a remote Git tag, you can also use the “git push” command and specify the tag name using the refs syntax.


Video Answer


2 Answers

dst refspec 0.2.0.0 matches more than one

Normally this isn't a problem but here git doesn't know which 0.2.0.0 this refers to, the solution is to be more explicit.

To delete the branch

To take care of the scenario in the question:

$ git push origin :refs/heads/0.2.0.0

To delete the tag

And for completeness, if it was the tag that was unwanted:

$ git push origin :refs/tags/0.2.0.0
like image 83
AD7six Avatar answered Oct 28 '22 22:10

AD7six


The exact meaning of the "refspec" argument to "git push" is quite complicated, and discussed at length in the git manual.

The most relevant part is this:

If doesn’t start with refs/ (e.g. refs/heads/master) we will try to infer where in refs/* on the destination <repository> it belongs based on the type of <src> being pushed and whether <dst> is ambiguous.

In other words, naming just the branch or tag is actually short-hand for naming a specific pointer in the "refs/" directory of the git repository.

To be more explicit, you can point to a specific entry in that directory:

  • Branches are stored in "refs/heads/", so "refs/heads/ABC" refers unambiguously to a branch
  • Tags are stored in "refs/tags/", so "refs/tags/ABC" refers unambiguously to a tag
  • Other refs can be used for other purposes; for instance, Github uses "refs/pull/123/head" and "refs/pull/123/merge" to refer to the current tip, and current merge result, of open Pull Request "123"
like image 25
IMSoP Avatar answered Oct 28 '22 23:10

IMSoP