Cleaning GIT Branches on your local

There are several code collaboration environments and one of it that I love the the most and is widely used these days is GIT repository; normally these GIT repositories are hosted on platforms like GITHUB or BitBucket. More or less the working of these GIT repository hosting platforms are similar.

This blog post is going to help developers who want to clean up their local code repositories when the remote hosted version of these repositories are deleted. One of my client's project was hosted on BitBucket where after 6 months of robust development my local environment was showing a huge list of branches which obviously got merged and removed from BitBucket. In order to make a quick cleanup I followed following steps.

Step # 1: Get list of all branches on local

To get a list of all branches in local; following command is run which will give a clear picture that how many branches linked to remote branches on local instance. 

# git branch -av

release-3.1                                   74ea551b Merged in Event-Content-tags-stripping (pull request #75)
release-3.10                                 c5dec754 [behind 18] Merged in newsletter-tweaks (pull request #168)
release-3.11                                 ec939437 Merged in coronavirus-intro-block (pull request #201)
release-3.12                                 cb00784 Merged in newsletter-tweaks (pull request #230)
release-3.14                                 c5920774 Merged in deploy (pull request #265)
release-3.15                                 795196fc Devices.php edited online with Bitbucket
release-3.16                                 66c75a5a Merged in push-notification (pull request #296)
release-3.17                                 0d6992e6 Merged in webpage (pull request #314)
release-3.18                                 88b49c87 Merged in front-tweaks-2 (pull request #339)
release-3.19                                 0ebfca17 Merged in paragraph-1 (pull request #370)
release-3.2                                   6cf8f84 Merged in removal-of-wrong-date-from-event-pages (pull request #88)
release-3.20                                 ee22cd7 Merged in news-redesign (pull request #408)
release-3.3                                   a62a5ac Merged in alakhbar-2020 (pull request #130)
release-3.4                                   8b3b46f Merged in ots (pull request #141)
release-3.5                                   9d06317 Merged in board-and-portfolio-pages (pull request #156)
release-3.6                                   31008f2 Merged in newsletter-responsiveness (pull request #158)
release-3.7                                   e67cc82 Merged in newsletter-tweaks (pull request #160)
release-3.8                                   9d8bfb4 Merged in newsletter-tweaks (pull request #162)
release-3.9                                   6218fad Merged in funeral-note (pull request #166)

Step # 2: Get a list of branches that does not exist on BitBucket

To get a list of all branches in local that are no more available of BitBucket most likely these branches were merged and removed over a passage of time; following command is run to get a list of those branches.

# git remote prune origin --dry-run

Pruning origin
URL: https://username@bitbucket.org/xyz/xyz.git

* [would prune] origin/release-3.1
* [would prune] origin/release-3.10
* [would prune] origin/release-3.11
* [would prune] origin/release-3.12
* [would prune] origin/release-3.13
* [would prune] origin/release-3.14
* [would prune] origin/release-3.15
* [would prune] origin/release-3.16
* [would prune] origin/release-3.17
* [would prune] origin/release-3.18
* [would prune] origin/release-3.19
* [would prune] origin/release-3.2
* [would prune] origin/release-3.20
* [would prune] origin/release-3.3
* [would prune] origin/release-3.4
* [would prune] origin/release-3.5
* [would prune] origin/release-3.6
* [would prune] origin/release-3.7
* [would prune] origin/release-3.8
* [would prune] origin/release-3.9

Step # 3: Remove branches that does not exist on BitBucket

# git remote prune origin

Pruning origin
URL: https://username@bitbucket.org/xyz/xyz.git

* [pruned] origin/release-3.1
* [pruned] origin/release-3.10
* [pruned] origin/release-3.11
* [pruned] origin/release-3.12
* [pruned] origin/release-3.13
* [pruned] origin/release-3.14
* [pruned] origin/release-3.15
* [pruned] origin/release-3.16
* [pruned] origin/release-3.17
* [pruned] origin/release-3.18
* [pruned] origin/release-3.19
* [pruned] origin/release-3.2
* [pruned] origin/release-3.20
* [pruned] origin/release-3.3
* [pruned] origin/release-3.4
* [pruned] origin/release-3.5
* [pruned] origin/release-3.6
* [pruned] origin/release-3.7
* [pruned] origin/release-3.8
* [pruned] origin/release-3.9

Step # 4: Check Status of pruned Branches on local GIT repository

All the branches that were removed on remote host will show an indication [gone] on branches with orphan status. in Step # 5 we will remove all those branches with [gone] indications.

# git branch -av

release-3.1                       74ea551b [gone] Merged in Event-Content-tags-stripping (pull request #75)
release-3.10                      c5dec754 [gone] Merged in newsletter-tweaks (pull request #168)
release-3.11                      ec939437 [gone] Merged in coronavirus-intro-block (pull request #201)
release-3.14                      c5920774 [gone] Merged in deploy (pull request #265)
release-3.15                      795196fc [gone] Devices.php edited online with Bitbucket
release-3.16                      66c75a5a [gone] Merged in push-notification (pull request #296)
release-3.17                      0d6992e6 [gone] Merged in webpage (pull request #314)
release-3.18                      88b49c87 [gone] Merged in front-tweaks-2 (pull request #339)
release-3.19                      0ebfca17 [gone] Merged in paragraph-1 (pull request #370)
release-3.2                       6cf8f84f [gone] Merged in removal-of-wrong-date-from-event-pages (pull request #88)
release-3.20                      ee22cd7d [gone] Merged in news-redesign (pull request #408)
release-3.3                       d005974c [gone] Merged in alakhbar-2020 (pull request #125)
release-3.4                       7fe497a7 [gone] Merged in safari-tweaks (pull request #140)
release-3.5                       194aeb2e [gone] Merged in responsive-newsletter-template-revert (pull request #146)
release-3.6                       31008f2d [gone] Merged in newsletter-responsiveness (pull request #158)
release-3.7                       71112a28 [gone] Merged in release-3.6 (pull request #159)
release-3.8                       114b6eff [gone] Merged in release-3.7 (pull request #161)
release-3.9                       693c12d7 [gone] Merged in release-3.8 (pull request #164)

Step # 5: remove local branches with no link to remote branch on BitBucket

# git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done

Deleted branch release-3.1 (was 74ea551b).
Deleted branch release-3.10 (was c5dec754).
Deleted branch release-3.11 (was ec939437).
Deleted branch release-3.14 (was c5920774).
Deleted branch release-3.15 (was 795196fc).
Deleted branch release-3.16 (was 66c75a5a).
Deleted branch release-3.17 (was 0d6992e6).
Deleted branch release-3.18 (was 88b49c87).
Deleted branch release-3.19 (was 0ebfca17).
Deleted branch release-3.2 (was 6cf8f84f).
Deleted branch release-3.20 (was ee22cd7d).
Deleted branch release-3.3 (was d005974c).
Deleted branch release-3.4 (was 7fe497a7).
Deleted branch release-3.5 (was 194aeb2e).
Deleted branch release-3.6 (was 31008f2d).
Deleted branch release-3.7 (was 71112a28).
Deleted branch release-3.8 (was 114b6eff).
Deleted branch release-3.9 (was 693c12d7).