Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git : How to delete a remote branch whose name starts with a hashtag '#'?

Tags:

git

gitlab

I've created a remote branch whose name starts with a hashtag mark and I've quickly learnt it's a bad idea as # is considered as a comment mark by git.

Therefore, I would like to delete that branch now but I can't find a proper way to do so… I've tried :

git push origin --delete <#branch_name>
git push origin --delete -- <#branch_name>

But git always returns this error message

fatal: --delete doesn't make sense without any refs.

So how can I walk around that issue ?

like image 475
Getter Avatar asked Sep 25 '17 09:09

Getter


3 Answers

Escape the #:

git push origin --delete \#branch_name
                         ↑
like image 151
Maroun Avatar answered Oct 24 '22 05:10

Maroun


You can delete any branch by this command

$ git push origin +:refs/heads/#branch_name

or

git push origin :<#branch_name>
like image 8
R.A.Munna Avatar answered Oct 24 '22 06:10

R.A.Munna


For this example, I will show how to delete the remote and local branches. If you have any doubts... SAVE a backup of your data! Normally, local and remote branch names would be identical, but I am adding a prefix "local-" and "remote-" to help clarify what is being deleted and where. Note, you cannot delete the default branch. (If you wish to, you must select another default branch first).

Say your default branch name on the remote github website for a repository called "myrepo" is: "remote-main", and the remote branch you wish to delete is "remote-subbranch".

On your local machine "myrepo" repository these are tracked with "local-main", and you wish to delete "local-subbranch" from the local repository.

Do:

In a git bash within the local repository on your machine:

your.name@identifiers123abc MINGW64 /path/to/your/local/myrepo (local-main)
$

check to see all local and remote branches that exist

$ git branch -a
*local-main
 local-subbranch
remotes/myrepo/remote-main
remotes/myrepo/remote-subbranch

remove local-subbranch if desired

$ git branch -d local-subbranch
Deleted branch local-subbranch

remove remote-subbranch if desired

$ git push -d remote-main remote-subbranch
To https://github.com/yourGHname/myrepo.git
 - [deleted]    remote-subbranch

check all branches again to confirm

$ git branch -a
*local-main
 remotes/myrepo/remote-main
like image 1
MJ_ Avatar answered Oct 24 '22 07:10

MJ_