Removing Merged Local Branches
$ git branch --merged origin/develop | grep -v develop | xargs git branch -d
Let's break down the command step by step:
git branch --merged origin/develop: This command lists all local branches that have been merged into the remote branch
origin/develop. It retrieves a list of branches that have their commits fully merged into the specified remote branch.
grep -v develop: The output from the previous command is then piped (
grep -v develop, which filters out the branch named "develop" from the list. The
grepis used to invert the matching, so it excludes any branch that matches "develop".
xargs git branch -d: The filtered list of branches is then passed as arguments to
xargs, which in turn executes the command
git branch -dfor each branch.
git branch -dis used to delete the specified local branch.
In summary, the overall command
git branch --merged origin/develop | grep -v develop | xargs git branch -d identifies all local branches that have been merged into the remote branch
origin/develop, filters out the "develop" branch from that list, and proceeds to delete each of the remaining branches. This is useful for removing merged feature branches that are no longer needed locally.