Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clean up my local branches if they are deleted from GIT repo

Tags:

git

github

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 .

like image 688
Ashish Agarwal Avatar asked Dec 02 '25 09:12

Ashish Agarwal


2 Answers

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.

like image 143
Rohit Poudel Avatar answered Dec 04 '25 01:12

Rohit Poudel


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.

like image 34
Shaleen Avatar answered Dec 04 '25 00:12

Shaleen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!