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 branchorigin/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 (|
) togrep -v develop
, which filters out the branch named "develop" from the list. The-v
option ingrep
is 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 toxargs
, which in turn executes the commandgit branch -d
for each branch.git branch -d
is 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.