标签云

微信群

扫码加入我们

WeChat QR Code

I'm not sure if this is something supported by Git, but in theory it seems like it should work to me.

My workflow often involves my editing of files in multiple branches simultaneously. In other words, I often want to open a few files in one branch is while I edit the contents of another file in another branch.

My typical solution to this is to make two checkouts, but it's a shame I can't share branches and refs between them. What I would like is to just have two working directories managed by the same .git folder.

I'm aware of local git clone solutions (the default, which is to hardlink shared objects, and the --shared option, which sets up an alternate object store with the original repo), but these solutions only cut down on disk space usage, and especially in the case of --shared, seem fraught with peril.

Is there a way to use one .git folder, and have two working directories backed by it? Or is Git hardcoded to have just one working directory checked out at any time?


git-new-workdir will be replaced by git checkout --to=<path> in Git 2.5. See my answer below

2018年08月15日25分05秒

Actually, the command will be git worktree add <path> [<branch>] (Git 2.5 rc2). See my edited answer below

2018年08月16日25分05秒

you should change the accepted answer VonC's answer, since things have changed since your originally asked the question.

2018年08月15日25分05秒

thanks for the updated answer!

2018年08月16日25分05秒

This is the coolest thing they've created, just what I was looking for. Thanks for that!

2018年08月16日25分05秒

How to delete the working tree only and still keeping the branch

2018年08月15日25分05秒

DotnetRocks you can delete any working tree (a local folder on your computer): that won't have any influence on the branch: the main .git repo will still include the full committed history, with all its branches, whether or not the working tree has been deleted.

2018年08月16日25分05秒

Yea, but if simply delete the working tree by going to that folder on my system and delete then git doesn't let me checkout to that branch and says that already checkout at <path> (<path> worktree path). But looks like if i do following : rm -rf <path> git worktree prune then it works . Is that right ?

2018年08月15日25分05秒

Jayan Thank you. I have made it clearer that 2.5 and 2.7 are now out.

2018年08月16日25分05秒

+1 I stand corrected, this is pretty awesome. It appears to instantly share history and branches between two different checked out repositories with no pushing/pulling, only symlinking. I was completely unaware that git could handle this. Alas, it isn't included in my distribution.

2018年08月15日25分05秒

For those using msysgit (windows) you can use this ported version of the script: github.com/joero74/git-new-workdir

2018年08月15日25分05秒

Usually it works nice, but if you accidentally edited the same branch in different locations, it is nontrivial to fix the things back.

2018年08月16日25分05秒

For those stuck on Git < 2.5 and who have submodules, try git-new-workdir-recursive which is a wrapper for git-new-workdir.

2018年08月15日25分05秒

This is exactly what I was looking for, but I can't get it to work... I'm getting "fatal: Not a git repository: '<localgitdir>'". Any ideas?

2018年08月16日25分05秒

Nevermind, turns out I was using "~" in my directory name, and git didn't like that. When I used the full path, it worked fine.

2018年08月16日25分05秒

DanR, glad it helped. :) You might also use $HOME. There is one other caveat about the above method that I discovered later, which has to do with files that don't exist in one or another branch. If you checkout A into dir1, then checkout B into dir2, then force checkout C into dir1, if there is a file that exists in A but not in B or C, the file won't be removed from dir1 even by the force checkout. So you may need to experiment with git clean in such a case—or do what I did, and simply only use this method to populate a freshly created directory.

2018年08月16日25分05秒

Thanks for the tip. I'm going to be wrapping this as part of a CLI tool in ruby anyway, so I'll make sure it always starts from scratch.

2018年08月16日25分05秒

DanR, it might interest you to view the code I was writing at the time. Most of it is generally applicable to any git staging script, with the exception of the CFEngine syntax checks. (Perhaps you might replace that with Ruby syntax checks.) :)

2018年08月16日25分05秒

Hi. The guy above shared a cool solution, the git worktree command. It works more nicely than clone several times the same repository. Try it out, you will like this new feature.

2018年08月15日25分05秒