Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prune local tracking branches that do not exist on remote anymore

Tags:

git

With git remote prune origin I can remove the local branches that are not on the remote any more.

But I also want to remove local branches that were created from those remote branches (a check if they are unmerged would be nice).

How can I do this?

like image 731
Alex Avatar asked Oct 25 '12 08:10

Alex


People also ask

How do you remove all branches that are not on remote?

Remove All Local Branches not on Remote 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.

How do I prune local branches?

Prune/Cleanup the local references to remote branch An option --dry-run is needed. Now go ahead and actually prune/cleanup the local references by running the command git remote prune origin . Note that you don't need an option --dry-run . Again, run the command git branch -a will show the local status of branches.

How do I delete a remote tracking branch?

Steps to delete remote Git branchesIssue the git push origin –delete branch-name command, or use the vendor's online UI to perform a branch deletion. After the remote branch is deleted, then delete the remote tracking branch with the git fetch origin –prune command.

Will deleting local branch affect remote?

Local branches are branches on your local machine and do not affect any remote branches. git branch is the command to delete a branch locally. -d is a flag, an option to the command, and it's an alias for --delete .


2 Answers

If you want to delete all local branches that are already merged into master, you can use the following command:

git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d 

If you are using main as your master branch, you should modify the command accordingly:

git branch --merged main | grep -v '^[ *]*main$' | xargs git branch -d 

More info.

like image 34
jackocnr Avatar answered Oct 16 '22 01:10

jackocnr


After pruning, you can get the list of remote branches with git branch -r. The list of branches with their remote tracking branch can be retrieved with git branch -vv. So using these two lists you can find the remote tracking branches that are not in the list of remotes.

This line should do the trick (requires bash or zsh, won't work with standard Bourne shell):

git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d 

This string gets the list of remote branches and passes it into egrep through the standard input. And filters the branches that have a remote tracking branch (using git branch -vv and filtering for those that have origin) then getting the first column of that output which will be the branch name. Finally passing all the branch names into the delete branch command.

Since it is using the -d option, it will not delete branches that have not been merged into the branch that you are on when you run this command.

like image 50
Schleis Avatar answered Oct 16 '22 01:10

Schleis