13

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 .

Ashish Agarwal
  • 139
  • 1
  • 5
  • 2
    you mean local branches or remote tracking branches on you machine? – dee zg Jun 20 '17 at 04:18
  • Possible duplicate of [How do I delete a Git branch both locally and remotely?](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely) – Doryx Jun 20 '17 at 04:19
  • 1
    My question is different from this. Thanks for the help. – Ashish Agarwal Jun 20 '17 at 05:25
  • Possible duplicate of [cleaning up old remote git branches](https://stackoverflow.com/questions/3184555/cleaning-up-old-remote-git-branches) – LuFFy Jun 20 '17 at 06:24
  • 1
    This is just reverse I am asking for. – Ashish Agarwal Jun 20 '17 at 06:38
  • Does this answer your question? [remove branches not on remote](https://stackoverflow.com/questions/16590160/remove-branches-not-on-remote) – esmail Mar 22 '23 at 21:01

4 Answers4

13

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,793
  • 2
  • 20
  • 24
6

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
  • 47,248
  • 30
  • 103
  • 119
  • 5
    Local copies of deleted branches are not removed by this. – Ashish Agarwal Jun 20 '17 at 05:16
  • 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 (: – user8395964 Jul 11 '23 at 10:03
3

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|dev)" | xargs git branch -d
Shaleen
  • 829
  • 7
  • 17
1

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
  • 448,244
  • 59
  • 642
  • 775