Let's say I had a branch named coolbranch
in my repository.
Now, I decided to delete it (both remotely and locally) with:
git push origin :coolbranch git branch -D coolbranch
Great! Now the branch is really deleted.
But when I run
git branch -a
I still get:
remotes/origin/coolbranch
Something to notice, is that when I clone a new repository, everything is fine and git branch -a
doesn't show the branch.
I want to know - is there a way to delete the branch from the branch -a
list without cloning a new instance?
Deleting remote branches To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .
Note that deleting the remote branch X from the command line using a git push will also remove the local remote-tracking branch origin/X , so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p . However, it wouldn't hurt if you did it anyway.
First, use the git branch -a command to display all branches (both local and remote). Next, you can delete the local branch, using the git branch -d command, followed by the name of the branch you want to delete.
This is because "git pull" does not remove remote tracking branches for branches deleted from remote repo.
git remote prune origin
, as suggested in the other answer, will remove all such stale branches. That's probably what you'd want in most cases, but if you want to just remove that particular remote-tracking branch, you should do:
git branch -d -r origin/coolbranch
(The -r
is easy to forget...)
-r
in this case will "List or delete (if used with -d
) the remote-tracking branches." according to the Git documentation found here: https://git-scm.com/docs/git-branch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With