Jii
I have been learning git and how to use remote branches on GitHub. I had a remote branch origin/other-branch and merged to update the branches, so when I put
git log --oneline --graph --all
I get the following output
- * 352a7d7 (HEAD -> master, origin/master) merge with other
- * 1da6f0a (origin/other-branch) other branch
- * 2b02a4b another commit
So I wanted to remove origin/other-branch but I didn't know how to do it, so I did it from GitHub (the website)
Then, I researched how to remove remote branches (haha, I think I should have done that first) and I get the following command:
git push origin --delete other-branch
What does it mean to send the deletion of the branch, but since it is no longer on the remote I get the message that it cannot find such a branch
error: unable to delete 'other-branch': remote ref does not exist error: failed to push some refs to 'https://github.com/myUserName/remote.git'
so how do I remove this remote branch? Thank you!
It is worth mentioning that this branch was sent by another user, since I no longer want to work on it, I want to delete it from my local
When you work with remotes on git, your local repository is entirely separate from the remote, and only gets entangled with remote branches when you say so.
The
git fetchcommand is important here (documentation). When you rungit fetch, it doesn't really change anything in your local repository. Rather, you can think of it as fetching the information about the remote and its branches, and exposing them to you. If you have fetched a remote,git branch -awill show you all your branches, including those on the remote.But important to note is that none of those remote branches exist in your local repository.
git branchwill show you all your local branches. Let's say this is your situation:git branch -awill list all those branches, but only the local ones exist in your repository.You can set a local branch to "track" a remote one, which can be done a couple of different ways. If you run
git checkout stagingin the above state, git will say "I don't see a staging branch here, but I see that there is one onorigin, so I'll set up a local branch with the same name for you and set it to trackorigin/staging." If you create a branch locally, you push it with the commandgit push -u <remote> <branch-name>, where-umeans upstream (the name of the remote you wish to track it to), and<branch-name>will be the name of the branch on the remote. That can be different from your local name, but it's usually easier if they're the same. I have aliasedgit push -u origin `git branch --show-current`because it's such a common command. If a branch with that name doesn't exist on the remote, it will be created, but if it already exists, git will simply start tracking your local branch to it.Now if the state is this:
It should now be more clear what's actually going on. There are 8 branches here: 4 local, and 4 remote. If you go to GitHub and delete a branch there, it will only delete the remote branch - it would be pretty awful if remote branches could reach into your local repository and delete your stuff! But your local repository won't know about the changes made on GitHub until you tell it to grab the remote's info - with
git fetch origin. Then,origin/branch-nameshould stop showing up.Another way to delete a remote is with the command you mentioned,
git push --delete <remote> <branch-name>. Because you run that from your local repository, you won't need togit fetchto update, but your local branch will still be around. That means you still need to delete your local branch usinggit branch -d <branch-name>.On the flip side, if you delete a branch locally, its tracked remote branch will happily keep existing. If you want to totally clear the project of this branch, you will have to do both. But take care to ensure that no one else is using it before deleting remote branches!