Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete branches listed by git branch -a

Tags:

The command git branch -a lists a bunch of branches that are NOT on the repository, and NOT local branches. How can these be deleted?

* develop   master   remotes/origin/cloner 

For example, remotes/origin/cloner used to exist in the repo, but it has since been deleted and I'd like it not to appear when typing git branch -a.

like image 615
Soroush Hakami Avatar asked Nov 01 '11 11:11

Soroush Hakami


People also ask

How do you delete branches in git?

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 unwanted branches?

Use git prune to remove orphaned/unused branches If you see any branches in there that you don't want, you can use the command git branch -d <branch name> . If you want to delete the branch called badbranch, use the -D switch to force the deletion if it doesn't work: git branch -d badbranch .

Does git prune delete branches?

git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.

How do I remove all remote branches?

Let's break this command: First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.


1 Answers

If you have remote-tracking branches (such as origin/cloner in this case) which are left over after the corresponding branch has been deleted in the remote repository, you can delete all such remote-tracking branches with:

 git remote prune origin 

The documentation for git remote explains this as:

Deletes all stale remote-tracking branches under <name>. These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/<name>".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

like image 123
Alexander Gladysh Avatar answered Sep 18 '22 06:09

Alexander Gladysh