Remove merged branches from your local machine
If you work with a remote Git repository, you might encounter a situation where you still have branches on your local machine, but those branches have been removed from the remote repository.
Open a bash terminal (if you are on Windows you will have git bash. CMD, and PowerShell will not work).
-
Run the following command:
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
Let’s break down the command:
git fetch -p: This command fetches the branches and their commits from the remote repository to your local repository. The -p or --prune option removes any remote-tracking references (i.e., origin/branch-name) to branches that have been deleted on the remote repository.
&&
: This is a logical AND operator. In the context of command-line operations, it means “execute the following command only if the previous command succeeded.”
git branch -vv
: This command lists all local branches and their upstream branches. If a local branch’s upstream branch has been deleted, it will show [gone] next to it.
An example where deleted-branch
is deleted, only-local-branch
was never pushed to the remote repository, and main
and test-branch
are both local and remote.
|awk '/:gone]/{print $1}'
: This part of the command pipes (|) the output of the previous command to awk, a text processing utility. The awk command is programmed to match lines containing :gone]
and print the first field ($1) of those lines, which is the branch name.
The result of git fetch -p && git branch -vv | awk ‘/: gone]/{print $1}’
given me only deleted-branch
| xargs git branch -d
: This part of the command pipes the output of the previous command (i.e., the names of the branches to be deleted) to xargs, which executes the git branch -d
command for each input. The git branch -d
command deletes a branch.
So, in summary, this command fetches updates from the remote repository, identifies local branches whose upstream branches have been deleted, and deletes those local branches.
Bonus
This is the error if you try to run it during a merge