1

What I am used to in the main worktree land:

  • I want to work on my co-workers branch as they are on holiday

on branch main, I run git pull origin/main, this fast forwards and pulls all refs down to my local. My co-worker's branch is newFeature, so I run git checkout newFeature and I am on that branch!

How do I do the same when working with a bare repo and git worktrees? I cannot figure out how to checkout newFeature locally, such that I have a folder right beside my main

SaiBorg
  • 326
  • 2
  • 14

1 Answers1

2

If you need multiple working directories with Git, you can:

  • clone your bare repository

  • from within that cloned repo folder, type:

    git worktree add -b newFeature ../newFeature origin/newFeature
    

That will create a newFeature for that branch, beside your local repo folder.


The OP Saiborg confirms in the comments:

In order for me to do what I wanted, I just had to do this:

git worktree add -b feature/newFeature newFeature origin/feature/newFeature 

which worked as expected.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I am in the bare repo (`.bare`, `main` folders I can see when I run `ls -a`). Are you suggesting I run this in this context? If I am not wrong, wouldn't this create a folder adjacent to the bare repo and not in the bare repo like how `main` is? Is there a way to do this such that, `main`, `newFeature` are at the same level as such, `myCloneBareRepo/main`, `myCloneBareRepo/newFeature`? – SaiBorg Jan 20 '23 at 01:20
  • @SaiBorg I was suggesting that you clone first the bare repository, then execute the git worktree command in the cloned repository. – VonC Jan 20 '23 at 06:54
  • That is what I have done! Also comment from last night just after maintenance: paste in so as it is down for maint I think I have figured it out! In order for me to do what I wanted, I just had to do this: `git worktree add -b feature/newFeature newFeature origin/feature/newFeature` which worked as expected. @VonC – SaiBorg Jan 20 '23 at 21:02
  • 1
    @SaiBorg Well done, thank you for the feedback. I have included your comment in the answer for more visibility. – VonC Jan 20 '23 at 21:12