Thursday, 24 October 2019

Git Worktrees

Since git 2.8 it is possible to work with "Worktrees". These are extra maps you can use to checkout a branch. This is instead of changing your current working directory contents by checking out a different branch (and by extention messing up your build targets and artifacts and stuff).

At work, unfortunately, we are using Fedora Core 25 (a little old, but we are sure to update any day now).

So I decided to install git from source1. Apparently we're already up to version 2.23.0

At home, I do not use worktrees2, and I managed to avoid it at work too. But the situation has progressed in such a fashion that the branches are starting to differ soo much that a reset maven profiles/clean/rebuild/redeploy is required when switching branches, costing me valuable development time.

Creating a new worktree from the master

git worktree add -b feature_branch ../new_folder

Creating a new worktree from the master from an existing branch

git worktree add ../new_folder feature_branch

Use --detach instead of -b branch to create a worktree without a branch.

Gebruik --detach ipv -b branch om een losgekoppelde worktree aan te maken zonder een branch.

Removal of a worktree

rm -rf ../new_folder && git worktree prune

It all seems easy enough.

For some reason the new git also enables me to simply checkout the svn repo (we're still using SVN at the moment) without any pains like in the past (for example bombing out because it takes too long). Perhaps something has changed in our SVN setup, I don't know.

References

[1] Git - see kernel.org link on the page
https://git-scm.com/download/linux
[2] Git - manpage worktree
https://git-scm.com/docs/git-worktree
StackOverflow - What would I use git-worktree for?
http://stackoverflow.com/questions/31935776/what-would-i-use-git-worktree-for
Atomic Object - Parallelize Development Using Git Worktrees
https://spin.atomicobject.com/2016/06/26/parallelize-development-git-worktrees/
Atlassian - Six cool features of the Git 2.x series
https://developer.atlassian.com/blog/2015/10/cool-features-git-2.x/
GAUI.SI - Git worktree feature
https://www.gaui.is/git-worktree-feature/

No comments:

Post a Comment