Delete a remote branch
Created: Last updated:
How do you delete a remote branch on GitHub? I found quite a few answers but all have failed. The problem is most likely the use of origin.
In tutorials and examples the use of "origin" is very convenient but it is ambiguous and quite fatal if you don't know what "origin" is or stands for.
Push origin
Lets go through some of the answers I found and who failed. The first answer that sounded promising looked like this
- $ git push origin :serverfix
- // serverfix in this example would be the branch name
I found this in the stackoverflow forum and it got so many votes that I thought it must work but look what I got instead for my "library" branch
- $ git push origin :library
- fatal: 'origin' does not appear to be a git repository
- fatal: The remote end hung up unexpectedly
- // there is another command but I got the very same result
- $ git push origin --delete library
That doesn't look good, does it?
From my first experiments with git I knew that 'origin' must or should exist because that's how all the examples work. It is unfortunate that it is used in all the documentation and examples you find everywhere and the reference above from the forum is exactly such an example.
However, I already changed a few things with additional directories and branches. So, "What is origin?" has become the new question in the house.
What is origin?
The answer is in the Git User's Manual which comes with your git installation and found in the Git directory at ./doc/git/html/user-manual.html. Somewhere in there you will find this sentence:
Note that the name "origin" is just the name that git uses by default to refer to the repository that you cloned from.
At first that does not help but it dawned on me very quickly that 'origin' is probably just an alias and I definitely had to use something else and probably the actual repository name.
A git repository representation
As the error said "'origin' does not appear to be a git repository" and the name for my repository was of course something else: library—yes, the same as the branch but I don't believe that was the problem.
Anyway I tried the follow with the familiar result at first
- $ git push library :library
- fatal: 'library' does not appear to be a git repository
- fatal: The remote end hung up unexpectedly
- // and I also tried this but same result
- $ git push library.git :library
Somehow I knew I was on the right track, though. Just what is the correct representation for the repository in this command?
Somehow I also understood that a repository with GitHub is using a different string and in your account you can find a ssh link. The following for the library was mine and note the colon [:].
- git@github.com:awd-git/library.git
- // and how I used it
- $ git push ssh://git@github.com:awd-git/library.git :library
- ssh: connect to host github.com:awd-git port 22: Bad file number
- fatal: The remote end hung up unexpectedly
We are not there yet but the response looked familiar. The colon triggered a port connection request. So, what about a regular path, I thought.
- $ git push ssh://git@github.com/awd-git/library.git :library
- To ssh://git@github.com/awd-git/library.git
- - [deleted] library
Now I got it. A quick check on the webpage and sure enough the branch was finally gone on the server. Now, I am not sure if the ssh is really need or if it would work with html, too. I think it just requires some sort of valid representation for your library on the host. There are other representations that I have found like "remote_host" and "choose_remote_name"; maybe one of these would work as well.
Conclusion
If you read the documentation and examples you will find things like git push origin master
a lot. It gives the impression that this is some local to remote representation which it is not. In the examples 'origin' is very convenient but it is ambiguous and quite fatal for newbies to git what 'origin' is or stands for, I think.