Sometimes, we forgot about all branches we're creating when coding, bumping versions, and doing some releases, in result we can have a lot of useless branches in our local project... and it can become frustrating when you need to switch from a branch to another one.
Soooo, i made a really simple and short bash script to help me delete multiple branch at the same time called gbd
, like git branch delete
, yeahhh yeah i know, too lazy... and the code looks like this :
#!/bin/bash
# by d4rk3r
# this function will loop over the list of branches you provided
# and if the branch name is upper than 2
# it will delete it
#
# $1 can be a file_name or an <<EOF ... EOF
_del_branch_list(){
for bb in $(cat $1);
do
if [ ${#bb} -ge 2 ];
then
echo "[-] Deleting '$bb' branch..."
git branch -D "$bb" # we delete the branch
fi
done
}
# the main function
main(){
echo "[-] Deleting locals branches is my job"!
_del_branch_list $1
}
main $1
After saving that file and add an alias to it in your .bashrc
, you just have to work with two commands:
- First, i need to get the list of commands, and i can have that by hitting
git branch
, as an output, i can have :
chore/some-bump-here
chore/some-bump-here
chore/some-bump-here
chore/some-bump-here
chore/some-bump-here
chore/some-bump-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
feat/some-feature-here
* master
release/some-release-here
release/some-release-here
release/some-release-here
release/some-release-here
release/some-release-here
release/some-release-here
release/some-release-here
release/some-release-here
release/some-release-here
I just have to select multiple branches and copy that
- Second, i can jut call the bash script and pass what i just copy in parameters like this (and add at the end the EOF then press ENTER):
d@k:project$ gbd <<EOF
> release/some-release-here
> release/some-release-here
> release/some-release-here
> release/some-release-here
> release/some-release-here
> release/some-release-here
> release/some-release-here
> release/some-release-here
> release/some-release-here
>
> EOF
output :
[-] Deleting locals branches is my job
[-] Deleting 'release/some-release-here' branch...
[-] Deleting 'release/some-release-here' branch...
[-] Deleting 'release/some-release-here' branch...
[-] Deleting 'release/some-release-here' branch...
[-] Deleting 'release/some-release-here' branch...
[-] Deleting 'release/some-release-here' branch...
Source code : https://gist.github.com/Sanix-Darker/f93b7fc029fc82e37d53693eca8d8471
Have FUN !