GIT Repository housekeeping

Ankush Sawant
4 min readApr 8, 2021

We have a complex automation framework with 50+ heads churning their brains everyday to break AUT somehow :)
Lots of people joined the family in between and same number of people decided to move on.
At one day we realized that we are having a huge count of unused or stale branches left in our git repository. Now it was the time for some housekeeping and deleting those unused branches. That time we found that there are few ways to do it in a best and time saving way directly from Command (I generally prefer CLI always over any UI).

A. Delete Selected branches from Git Repository

Let’s look into my git branches:

remote has total 9 branches

Requirement is that I have to remove all the feature branches or the branches having a text ankush in them.

1.Dry Run: Before actually running delete command I would first like to check what it will delete and can makeup my mind to proceed.

git branch -r --list '*ankush*' | sed 's/origin\///'
it has returned me 4 matching branches

2. Get list in file: If needed I can grab the branches which will be deleted by the command in a Text File for get it reviewed with peers.

git branch -r --list '*ankush*' | sed 's/origin\///' >> branches.txt
remember branches are not yet deleted

3. Delete and verify: Once all set, to finally delete the selected branches we need to give below command. Post execution we can check the remote branches are actually deleted.

git branch -r --list ‘*ankush*’ | sed ‘s/origin\///’|xargs git push --delete origin
branches, 4 in numbers are deleted from remote (not from local)

B. Delete all branches from Git Repository except few

This is an opposite case. Here we need to delete all the branches except few branches which I got from my team as they still want them to be intact.

Again my remote is having same 9 branches as earlier

remote is having 9 branches for now

Requirement is I need to save only below 4 branches out of these 9 branches
origin/main
origin/master
origin/release/5.6
origin/release/5.7

1.Dry Run: Before actually running delete command I would first like to check what it will delete and can makeup my mind to proceed. We used RegEx here to filter out branches so used -E in grep and branch names which are to be preserved and are separated by pipes.

git branch -r | sed 's/origin\///' | grep -Ev "("master"|"main"|"release/5.6"|"release/5.7")"
required 4 branches are filtered out

2. Get List in File: As earlier If needed I can grab the branches which will be deleted by the command in a Text File for get it reviewed with peers.

git branch -r | sed 's/origin\///' | grep -Ev "("master"|"main"|"release/5.6"|"release/5.7")" >> branchname.txt
get the result in a text file

3. Delete and Verify: Once you decided to go ahead to finally delete the selected branches we need to give command.
Post execution we can verify the existing branches on git repo.

git branch -r | sed 's/origin\///' | grep -Ev "("master"|"main"|"release/5.6"|"release/5.7")"|xargs git push --delete origin
Filtered branches are saved rest deleted

COMMON ISSUES

  1. Mismatch in branches in remote and local:
    Sometimes execution of delete command will report error
    error: unable to delete ‘feature/ankush/test1fix’: remote ref does not exist
    This error says that remote and local repos are not in syc. To sync them execute below command first.
git fetch --prune

--

--

Ankush Sawant

Working as a Senior Lead with GlobalLogic. Passionate about learning new things. Likes humor and not an Covidiot.