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.
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 .
It is safe to delete your local branch after you pushed your changes to your own remote repository. The pull request is unrelated to this, because it is simply a request to the maintainers of the original repository to merge your changes back into their code base.
The command git branch -a
shows remote branches that exist in your local repository. This may sound a bit confusing but to understand it, you have to understand that there is a difference between a remote branch, and a branch that exists in a remote repository. Remote branches are local branches that map to branches of the remote repository. So the set of remote branches represent the state of the remote repository.
The usual way to update the list of remote branches is to use git fetch
. This automatically gets an updated list of branches from the remote and sets up remote branches in the local repository, also fetching any commit objects you may be missing.
However, by default, git fetch
does not remove remote branches that no longer have a counterpart branch on the remote. In order to do that, you explicitly need to prune the list of remote branches:
git fetch --prune
This will automatically get rid of remote branches that no longer exist on the remote. Afterwards, git branch -r
will show you an updated list of branches that really exist on the remote: And those you can delete using git push
.
That being said, in order to use git push --delete
, you need to specify the name of the branch on the remote repository; not the name of your remote branch. So to delete the branch test
(represented by your remote branch origin/test
), you would use git push origin --delete test
.
The meaning of remotes/origin/test
is that you have a branch called test
in the remote server origin
. So the command would be
git push origin --delete test
There's a shortcut to delete the branch in the origin:
git push origin :<branch_name>
Which is the same as doing git push origin --delete <branch_name>
git fetch # synchronize with the server
git branch --remote # list remote branches
origin/HEAD -> origin/master
origin/develop
origin/master
origin/deleteme
git push origin --delete deleteme
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