1

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 .

3 Answers3

4

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.

Rohit Poudel
  • 1,642
  • 2
  • 18
  • 20
2

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.

sfletche
  • 42,426
  • 25
  • 94
  • 115
0

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.

torek
  • 389,216
  • 48
  • 524
  • 664