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 remote prune origin
$ git fetch origin --prune
$ git fetch origin -p
$ git fetch --all --prune
$ git fetch -a -pThe 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 -dThis 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
No comments:
Post a Comment