One of the coolest features in Git. And simultaneously one of the least used.
The classic situation:
You're in the middle of working on feature/blubb_feature and then a colleague sends an MR asking you to quickly review hotfix/make_that_users_dont_die_immediately so it can go live ASAP. Unfortunately the branch is too complex to review entirely in GitLab.
So you actually need to check it out and dive deep into the code...
The classic approach
git stashyour changesgit checkout hotfix/...to look at the branch and go through everything- Approve or reject
A much better approach
You've set up git worktree (it's built into git) and have a folder structure like this:
project_name/
- main/
- develop/
- feature/
- blubb_feature/
- hotfix/
- make_that_users_dont_die_immediately/Each folder acts like its own Git repository, but they all share the same Git metadata. Instead of leaving your current state and switching entirely, a git worktree add BRANCH_NAME creates a folder pointing to that branch for you.
So how does it work?
Super simple, really :).
mkdir my_project(create project directory) and navigate into it:cd my_project- Clone the repository (note the first special step):
git clone --bare GIT_URL .bare- We only clone the bare repository without checking out a branch
- These metadata are stored in the
.barefolder (the leading.hides it on macOS/Linux)
- Create a file
my_project/.gitwith the following line:
This tells Git where to find the repository metadatagitdir: ./.bare - From here, worktree works perfectly:
git worktree add mainadds our main branchgit worktree add feature/ABC_new_stuff_to_play feature/ABC_new_stuff_to_playto check out or create branches with/in the name- You can also freely choose the folder location in the filesystem. The format is:
git worktree add PATH BRANCHNAME
More useful commands
- Delete a branch:
- Option 1:
rm -r BRANCH_NAMEthengit worktree prune - Option 2:
git worktree listlists all worktree branches
- Option 1:
- Move a branch:
git worktree move SOURCE DESTINATION