Git housekeeping 🧹

Milan Brankovic
3 min readJan 25, 2020

You did a lot of work, released a lot of features and fixed critical (and other non-critical) bugs. For each of those a new branch has been created and you now have an enormous list of branches which are either outdated, or merged to develop . Now is the perfect time to do a cleanup.

# Remove local branch(es)

First we need to find out which branches are merged and can be deleted. To obtain a list of those branches simply execute:

git branch --merged

Next we can delete those branches with:

git branch -d <branch name>// force delete branch
git branch -d -f <branch name>
// or
git branch -D <branch name>

This will remove only a branch specified with <branch name> . If you have a long list of merged branches it is convenient to remove them all in one sweep, like so:

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Note: this will remove all local branches that are already merged into the currently checked out branch.

There are also sets of local branches which are not merged and/or were used for experiments or other stuff. We can list those branches with:

git branch --no-merge

After deciding which branches can be safely deleted we can use delete command as described above. Note that you need to use "force" to do it.

# Remove remote branch(es)

Deleting a local branch doesn’t remove the remote branch. We need to do it ourselves (if not done automatically on server when pull request is merged).

git push --delete origin <branch name>
// or
git push origin :<branch name>

Of course there is one-liner to delete a bulk of branches

git branch -r --merged | egrep -v "(^\*|master|dev)" | xargs -n 1 git push --delete origin

The last thing we need to do is to remove stale references:

git remote prune origin

You can also do it by fetching, only by adding prune flag:

git fetch -p

The -p option tells Git to remove any remote-tracking references that no longer exists on the remote repository before fetching.

# Clean untracked files

// clean just untracked files
git clean -f
// ... and remove directories
git clean -f -d
// ... and remove ignored files
git clean -f -d -x

Be careful with this one, it deletes files permanently!

To see what should be done you can execute git clean -n first.

# Garbage collection

git gc

gc cleans up internal content in the repository by (a) compressing file revisions (to reduce disk space and increase performance) and (b) removing unreachable objects. For the best overall performance and usage of disk space it is recommended to run git gc on a regular basis.

# WIP removal

While working on some issue you decided to save some changes you did in order to get back to them when other more important issue is finished. The changes have been stash ed, but were not considered for use any more.

There can be many Work In Progress improvements put on the shelf, but not used. To check what needs to remain you need to list them first:

git stash list

Now you can see each individual stash and remove it if not necessary:

git stash show <stash>git stash drop <stash>

If none of the stashed changes are necessary you can discard them all:

git stash clear

Now everything should be neat again and you can build up a new thing!

--

--

No responses yet