Remove Local Git Branches | SJB -->

This website uses cookies to ensure you get the best experience. More info...


Remove Local Git Branches

Sometimes we fall in need that we need to remove all the local git branches that are not on the remote. We create a pull request after finishing our tasks and merge that pull into the master, we have an option to delete that particular sub-branch at that time.

Git Branch
Once we delete that sub-branch from the server, we can run any of the following commands to remove that branch from the remote tracking branch list_
$ git remote prune origin
$ git fetch origin --prune
$ git fetch origin -p
$ git fetch --all --prune 
$ git fetch -a -p
The last two commands will remove any existed obsolete tracking branches. Now we can run the following command to remove any local branches that do not exist in the remote branches.
$ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
This fancy git command lists all the remote branches in nice format then it prints the difference between the remote and the local branches and then performs a delete. By the way, if we want to check first what branches will be removed, we can run the command without the last part, which will be_
$ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}'
Happy coding!
Check out List all Modified File Names with Git .

No comments:

Post a Comment