Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a remote branch in a local git repository?

Tags:

git

All the remote branches are visible as remotes in my local git repository after a git fetch.

How to selectively remove the remote branches in my local repository (not in the remote repository)?

like image 664
Talespin_Kit Avatar asked Aug 04 '11 11:08

Talespin_Kit


People also ask

Can you delete a branch in a local and in a remote repository?

You'll often need to delete a branch not only locally but also remotely. To do that, you use the following command: git push <remote_name> --delete <branch_name>. The branch still exists locally, though.

How do I delete a branch in local?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

How do I delete a remote branch without deleting local branch?

Deleting Remote Branches Unlike local branches, you can't delete a remote branch using the git branch command. However, you need to use the git push --delete command, 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 .


2 Answers

I had a slightly different but similar issue, but the solution might be useful to others who stumble on this question...

I noticed that my local repository still had remote branches that no longer existed on the remote, so I wanted to remove them. The solution is simply to fetch with the --prune (or -p) option:

git fetch --prune 
like image 58
Thomas Levesque Avatar answered Oct 17 '22 09:10

Thomas Levesque


git branch -r -d remote/branch

You also need to reconfigure fetch to avoid fetching this branch again later

like image 41
Ilya Ivanov Avatar answered Oct 17 '22 10:10

Ilya Ivanov