19

How can I delete all my local branches if they are deleted from GIT repo. Is there any command for that ? . I dont want to it one by one by the command git branch -D/-d branch-name .

6
  • 2
    you mean local branches or remote tracking branches on you machine? Commented Jun 20, 2017 at 4:18
  • Possible duplicate of How do I delete a Git branch both locally and remotely? Commented Jun 20, 2017 at 4:19
  • 1
    My question is different from this. Thanks for the help. Commented Jun 20, 2017 at 5:25
  • Possible duplicate of cleaning up old remote git branches Commented Jun 20, 2017 at 6:24
  • 1
    This is just reverse I am asking for. Commented Jun 20, 2017 at 6:38

5 Answers 5

21

Remove information on branches that were deleted on origin

When branches get deleted on origin, your local repository won't take notice of that.

You'll still have your locally cached versions of those branches (which is actually good) but git branch -a will still list them as remote branches.

You can clean up that information locally like this:

git remote prune origin 

Your local copies of deleted branches are not removed by this.

The same effect is achieved by using

git fetch --prune 

You could also set that as a default.

Sign up to request clarification or add additional context in comments.

Comments

13

Just for reference if someone looking in this page later

Quiet good amount of details on the same

DevConnect Page

Command which we can use for fully merged local branches is

git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d 

This will list merged branches, grep for lines that are not master, main, or dev, then delete each of these branches.

Comments

7

To delete (or "prune") local branches that are not in the repo

git remote prune origin 

prune

Deletes all stale 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 no actually prune them.

2 Comments

Local copies of deleted branches are not removed by this.
yes, i was looking for this. git remote prune --dry-run origin was it for me. there was origin/branch in remote which got deleted (say, after a merge request), but the local (well, the local on codespace) was still showing it; this fixed it (:
2

It sounds like you are asking for a way to delete your own branch named train if there was an origin/train at one point and there is no longer an origin/train now.

This is (a) somewhat dangerous and (b) difficult to do (because there is nothing built in to remember that "there was an origin/train"), but if you redefine the problem a bit, it's much less difficult. It remains dangerous. It means you are telling your software to automatically destroy information whether or not that loses information you did not want destroyed. For instance, you may have put a lot of work in the last day or two into your train and then someone deletes the upstream origin/train not realizing that you are working on it. Now you tell your Git to delete your train without ever giving you a chance to restore origin/train, and you lose the work you just did.

(You can get your work back, through the HEAD reflog, but it's not a very good plan—which is why I call this "somewhat dangerous".)

To see ways to delete branches that have lost their upstreams (e.g., after running git remote prune origin or git fetch --prune origin), see Remove local branches no longer on remote.

Comments

2

If you need/want to do it in Powershell, you can use this

git branch --merged | ForEach-Object { git branch -d ($_).Trim() } 

It will delete all the branches merged with the currently checked out branch. The current branch will not be deleted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.