How to checkout multiple git branches at the same time

When working on the Nextcloud server and apps it is quite useful to run separate version branches besides each other. Until now I had cloned the server repository to separate folders, making it hard to share a commit between two major branches, since it always required a push to a remote first. Today I stumbled upon a git command that makes this fairly easy.

The command git worktree allows you to have separate working directories for multiple branches, that all use the same repository. Assuming that you are in the cloned directory of the server repository, the following command will checkout the stable13 branch at a new directory called stable13:

git worktree add ../stable13 stable13

After committing some changes in the main repo directory, you can easily cherry-pick those changes in the stable13 working directory.

Of course you can also do the same thing for any app, so it is much easier to check the compatibility to older server versions.

So why is this better than just switching branches? In terms of Nextcloud it is quite handy to have a separate directory that will be served by your webserver so you can check without migrating between versions or reinstalling your development setup all the time. On top of that you don’t store the git metadata multiple times (which is around 400MB for each clone of the Nextcloud server).

I hope that some of you might find this useful. It really improved my backporting/testing workflow a lot.


3 thoughts on “How to checkout multiple git branches at the same time”

Leave a Reply

Your email address will not be published. Required fields are marked *