To delete all Git branches that have been merged into the current branch, you can use the git branch
command in combination with some shell scripting. Here's how you can do it:
sh
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
Let's break down the command and provide an example:
git branch --merged
: This command lists all branches that have been merged into the current branch.grep -v "\*"
: This command filters out the current branch (denoted by an asterisk*
) from the list.xargs -n 1 git branch -d
: This command takes each branch name from the filtered list and runs thegit branch -d
command to delete the branch.
Here's an example scenario:
Assuming you have the following branches:
bash
* master feature/branch1 feature/branch2 bugfix/fix1
And let's say you're currently on the master
branch. To delete all branches that have been merged into master
, you can run the command:
sh
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
After running the command, the feature/branch1
, feature/branch2
, and bugfix/fix1
branches will be deleted since they have been merged into master
. The current branch, master
, will not be deleted.
Please be cautious when using this command, as it will permanently delete branches. Make sure you've confirmed that the branches you're deleting have been merged and are no longer needed before executing the command.
Comments
Post a Comment