How to switch branches between two separate worktrees in Git?
Asked Answered
H

2

7

I added a linked working tree for "develop" branch. So my worktrees are like this:

- "project" worktree -> master branch
- "project_develop" worktree -> develop branch

In this condition, what I want to do is to checkout master branch on "project_develop" worktree. But as I know, it is impossible to checkout same branch on separate worktrees. The only way I could think of is to add a new temporary branch.

I would appreciate it if someone show me another way.
Thanks!

Humerus answered 2/4, 2019 at 7:38 Comment(7)
It would be useful to know what you're trying to avoid / achieve here... what is the problem you foresee with a temporary branch?Charissacharisse
@RomainValeri there would be no problem adding a temporary branch, but the process is a bit inefficient I think, adding and deleting an extra branch just to switch existing branches. I was just wondering if there would be a better way.Humerus
I can't imagine any alternative being more "efficient" than a one line command... git checkout <hash> is not really quicker than git checkout -b temp <target>...Charissacharisse
Do you really need it? you can merge into master.Fledgy
Note that if you wish to keep an added work-tree around for a long period (more than two weeks), make sure your Git version is at least 2.15. Git versions before 2.15 (but 2.5 or later where git worktree first appeared) have a nasty bug where added work-trees can become corrupted after 14 days. (Exactly when this happens, and whether it happens at all, is not easy to predict. It has to do with a bug in git gc.)Mountain
@Mountain fortunately my git version is 2.16, thank you for the adviceHumerus
@Fledgy This kind of system might seem a bit awkward, but it's necessary in our development environment :(Humerus
L
9

The easiest way is, on the master worktree

git checkout --detach

There are numerous other ways; the point is, since you don't have another branch to checkout to this worktree, you want to force it into detached HEAD state. It can still have the same code checked out; it just can't think it's "on" the master branch (since there would be problems with updating the worktrees on commit).

Once that's done, on the 2nd worktree you can git checkout master, and then if you want you could even go back to the first worktree and git checkout develop to swap the branches entirely. Or just wait until you're ready to check develop back to the 2nd worktree, and afterward check master out on the 1st tree again...

Lach answered 2/4, 2019 at 9:32 Comment(0)
D
1

I think creating a temporary branch is best. If you don't want to create a temporary branch, you have other options.

  1. Checkout a specific commit. In your case, you can just run git checkout refs/heads/master. It leads to detached HEAD state. refs/heads/master can be replaced with the specific commit.

  2. Create as many worktrees for a detached HEAD as you want to. You can run git worktree add /path/foo1 refs/heads/master, git worktree add /path/foo2 refs/heads/master, ..., git worktree add /path/fooN refs/heads/master.

  3. Reset develop to master. In "project_develop" worktree, run git reset master --hard.

These approaches bypass the limitation that you can't create a second worktree for the same branch. The 1st and 2nd are nearly the same with a temporary branch. The 3rd may make you confused some time later.

Diplomatist answered 2/4, 2019 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.