Skip to content

Git

Delete local Git branches after squash merging

Squash merging pull requests seems to be the only sensible way to do things. This allows a developer to make as many commits to a branch that they want, and address code review feedback. At the same time, it helps the reviewer because the PR author does not need to constantly rebase a branch. Doing so would hinder a reviewer to know what has changed since the last review.

Git unfortunately gets confused because it cannot know which commits have been merged. Squash merging a branch results in a single commit with a new identity.

I try to make many small pull requests each containing a logical change. This has many benefits.

Every once in a while I notice how many local branches I have and need to do a cleanup. Here is how:

This only works if remote branches are deleted after merging a pull request. For example, on GitHub this can be done automatically via a repository setting.

First, prune the remotes:

git fetch --prune

Now, find all branches whose remote is gone:

git branch --verbose | grep "\[gone\]" | awk '{print $1}' | xargs git branch --delete

Git will give you an error because it thinks that the branches have unmerged commits. This step is just to ensure that the right branches are found. You don't want to delete a branch you want to keep by accident.

When you are satisfied, append --force:

git branch --verbose | grep "\[gone\]" | awk '{print $1}' | xargs git branch --delete --force

Move more than one directory into a new repository

I just realized that my previous post on how to move one directory from one repository to another really only works for one directory.

Fortunately, there is a very easy solution to that using a nice little tool called git_filter.

Basically follow the instructions of it's README. Then, all I did was put the two directories into the filter file. It is important to note here that this file had to end with an empty line in my case, otherwise the last directory will be ignored.

You will get a new branch, which can be pushed to an empty repository:

git remote add origin_repoB <url of repo>
git push origin_repoB <localBranch>:master

It also works for one directory and is a lot faster compared to the other method.