Monday, 18 February 2019

Script to remove all local git branches that have been merged


This is a bash script that removes all local git branches if:
- they have been merged
- they are not called develop
- they are not called master

It lists the branches and prompts me to confirm the list before the deleting starts.

deleting my local branches using the script

The script:

function remove_branches {
  echo "Removing all merged local branches!!!"
  for k in $(git branch --merged  | sed /\*/d); do 
    if [ -z $(echo $k | grep -i 'master\|develop') ]; then
      git branch -D $k
    fi
  done
exit
}


# Print branches to be deleted
for k in $(git branch --merged | sed /\*/d); do 
  if [ -z $(echo $k | grep -i 'master\|develop') ]; then
      echo $k
  fi
done


# Prompt user to delete branches
while true; do
read -p "Do you wish to remove these branches?" yn
case $yn in
[Yy]* ) remove_branches;;
[Nn]* ) echo "okay."; exit;;
* ) echo "Please answer yes or no."
esac
done

No comments:

Post a Comment

  Azure Trusted Signing Signtool Error SignTool Error: An unexpected internal error has occurred. Error information: "Error: SignerSign...