Over time, your local git branches list can become overwhelming, particularly if you develop on a single branch, generate a pull request, merge it into the main branch, and then remove the remote git branch once it has been merged. After the branch is removed from the remote repository, there is no need to keep it on your local machine.
The following git command will delete all local branches that have been merged into the main branch. If your git trunk branch is not named main or you wish to remove all branches that have been merged into a branch other than main, simply modify the two instances of the word main in the command to reflect the name of your branch.
List of git commands to remove local merged git branches
To remove all the local branches, which are merged into the main branch, navigate to the root of the repository and run the following git commands,
- Fetch the latest updates from the git repository
git fetch
- See the list of local branches available in the repository
git branch
- Delete all local branches that have been merged to main branch
git branch --merged main | grep -v "^\* main" | xargs -n 1 -r git branch -d
Explanation:
This command is a combination of several Git commands and shell commands that work together to delete all the local Git branches that have been merged into the main branch, except for the main branch itself.
Here’s a breakdown of the individual commands and what they do:
git branch --merged main: This command lists all the local branches that have been merged into themainbranch. The--mergedoption tells Git to only list branches that have been fully merged intomain.grep -v "^\* main": This command filters out themainbranch from the list of branches. The-voption tellsgrepto invert the match, i.e., show only the lines that do not match the pattern. The pattern in this case is"^\* main", which matches lines that start with an asterisk (\*) followed by the textmain.xargs -n 1 -r git branch -d: This command passes the list of merged branches (excludingmain) to thegit branch -dcommand, which deletes each branch. The-n 1option tellsxargsto pass one branch name at a time to thegit branch -dcommand. The-roption tellsxargsto only run thegit branch -dcommand if there is at least one branch name to pass to it. The-doption tells Git to delete the branches, but only if they have been fully merged into the current branch (in this case,main). Note that this option will fail if the branch has unmerged changes, in which case the-Doption could be used instead to force-delete the branch.
Want to learn such useful commands? Get a Mastering Git book from https://amzn.to/3Hp527B
List of git commands to remove local nonexistent git branches
Similarly, run the following git commands to remove all the deleted branches from the local computer:
- Fetch the latest updates from the git repository
git fetch
- See the list of local branches available in the repository
git branch
- Delete all local branches that have been merged to main branch
git branch -vv | grep ': gone]' | grep -v '\*' | awk '{ print $1; }' | xargs -r git branch -d
Explanation:
This command is a combination of several commands in a shell pipeline that work together to delete Git branches that are marked as "gone".
Here’s a breakdown of the individual commands and what they do:
git branch -vv: This command lists all the Git branches in the local repository, with additional information about their upstream branches (if any). The-vvoption adds more information about whether each branch is “up to date”, “behind”, or “ahead” of its upstream branch.grep ': gone]': This command filters the output of thegit branch -vvcommand to only show the branches that are marked as"gone". Thegrepcommand searches for lines that contain the text": gone]", which indicates that a branch is gone.grep -v '\*': This command further filters the output to exclude any branches that are currently checked out (indicated by the asterisk symbol). The-voption tellsgrepto invert the match, i.e., show only the lines that do not contain an asterisk.awk '{ print $1; }': This command extracts only the branch names from the filtered output. Theawkcommand splits each line of input into fields and prints only the first field, which is the branch name.xargs -r git branch -d: This command passes the branch names to thegit branch -dcommand, which deletes each branch. The-roption tellsxargsto only run thegit branch -dcommand if there is at least one branch name to pass to it. The-doption tells Git to delete the branches. Note that this option will fail if the branch has unmerged changes, in which case the-Doption could be used instead to force-delete the branch.
Note: you can test these git commands by creating temporary repository on any git platform like, GitHub, GitLab or Bitbucket.
Disclaimer: This post contains affiliate links. If you use these links to buy something, I may earn a commission at no extra cost to you.
