Created
April 5, 2021 14:22
-
-
Save vvkpd/392e349ad409d9bb494418bb190088b7 to your computer and use it in GitHub Desktop.
Delete unused Git branch from local and remote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# to get the --no-merged branches right before 2 month (echo branch name) | |
for branch in $(git branch -r --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-07-23") { print $NF}}' | |
# to get the --no-merged branches right before 2 month | |
for branch in $(git branch -r --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-07-23") { print }}' | |
# to get the --no-merged branches right before 2 month without release branches (echo branch name) | |
for branch in $(git branch -r --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-07-23") { print $NF}}' | awk '!/release/' | |
# to get the --no-merged branches right before 2 month without release and master branches (echo branch name without origin keyword) | |
for branch in $(git branch -r --list --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-07-23") { print $NF}}' | sed 's/origin\///g' | awk '!/release/' | awk '!/master/' | |
#to delete local branches | |
for branch in $(git branch -r --list --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-07-23") { print $NF}}' | sed 's/origin\///g' | awk '!/release/' | awk '!/master/' | xargs git branch -D | |
#to dump branch names into a file | |
for branch in $(git branch -r --list --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-07-23") { print $NF}}' | sed 's/origin\///g' | awk '!/release/' | awk '!/master/' >branchToBeDeleted.txt | |
#to delete branches from remote | |
cat branchToBeDeleted.txt | xargs git push origin --delete | |
# branches before 25-04-2020 without spike with commit date | |
for branch in $(git branch -r --list --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-04-25") { print }}' | sed 's/origin\///g' | awk '!/release/' | awk '!/master/' | awk '!/spike/' | |
#command we executed | |
# to capture branches with last commit date | |
for branch in $(git branch -r --list --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-04-25") { print }}' | sed 's/origin\///g' | awk '!/release/' | awk '!/master/' | awk '!/spike/' >branchesWithLastCommitDate.txt | |
# to capture branches that can be deleted | |
for branch in $(git branch -r --list --no-merged | grep -v HEAD); do echo -e $(git show --format="%ci %cr %an" $branch | head -n 1) \\t$branch; done | sort -r | awk '{ if($1 <= "2020-04-25") { print $NF}}' | sed 's/origin\///g' | awk '!/release/' | awk '!/master/' | awk '!/spike/' >branchesToBedelete.txt | |
# to delete captured branches | |
cat branchesToBedelete.txt | xargs git push origin --delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment