Useful Git Tricks

The following paragraphs are mostly personal notes, so that I do not forget about these.

Migrating Things Between Git Repositories

Recently I had to migrate parts of a git repository hosted on Bitbucket infrastructure over to other git repositories hosted on GitHub. I had to move whole folders as well as single files. One of the requirements was that I migrate things including their whole git history. So just copy and pasting things was out of question.

By default, git comes with a function called filter-branch. But its command line interface is rather complex and error-prone. In addition, the tool is famous for being extremely slow. A quick search revealed that there is a handy alternative, namely git-filter-repo

The tool git-filter-repo is sufficiently well documented and provides some nice examples on how to handle the most common tasks. In the end I followed the steps listed here but used git-filter-repo rather than filter-branch. It went pretty well, and once you know how to do it, it’s a matter of less than half an hour to move things around.

These are the steps that I did.

  1. Checkout your source and target repository and make sure both checkouts are clean.
  2. Apply git-filter-repo on your source repo. Keep in mind that this step will irreversibly alter your repository (that’s why you should work on a clean checkout).
  3. Switch to your target repository and set the source repository as an additional remote.
  4. Fetch the information from your new remote and merge the changes into your target repository. You might have to use the flag --allow-unrelated-histories while merging. This is not mentioned here, but I had to add the flag.
  5. Remove the source repository as remote and push your changes.
  6. Open a merge/pull request on your target repository to get the changes onto the main branch.

For the last step you have to keep in mind that you moved the whole git history. The merge/pull request will probably be large. In my case it contained about 3000 commits. It’s probably going to take a while until my colleagues have reviewed it.

Git Hooks

I recently had to set up a new project with some colleagues. We agreed to use commit hooks to automatically check for simple issues in our commits. We opted for pre-commit to manage our git hooks and gitlint to enforce conventional commits. Overall we were quite happy with the setup. Putting everything together was quite easy and so far the set up is working very well.

Git Worktrees

The last thing I have discovered recently are git worktrees. It’s very convenient if you have to work in parallel on several branches from the same repository. If you don’t know about this feature, have a look at its documentation.


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

Mathematician and
Software Engineer

Researcher, Engineer, Tinkerer, Scholar, Philosopher, and Hyrox afficionado