问题
For use with worktrees, I'm trying to make a bare clone but where remote branches are mapped to remote tracking branches.
I'm trying to set up a generic worktree workflow. My repo is really really big, so cloning it is expensive. And even using hard links, as repos get gc
ed, they tend to diverge over time and use up extra space, and a fetch in one repo is does not help another, so working in worktrees seems the obvious thing to do. (You'll have to trust me on this.)
Worktrees require a 'main' repo that actually holds the objects. Obviously, that clone must not be deleted, so I'd like to put it somewhere like /var/cache/git/reponame.wt.git
. (I'm making a generic solution for me and my colleagues, so I've got to make it fairly foolproof.)
I could make it a regular clone, but then it has to have a branch checked out and then that branch cannot be checked out by any other worktree, so I'd have to make a dummy branch (e.g. wt_dummy
) that could confuse people. What I'd really rather do is make the main repo a bare
repo.
The trouble is that git clone --bare
does not create remote tracking branches. So to make a "bare repo but with remote tracking branches", I've started on this path
git clone --bare <remote> repo.tmp
mv repo.tmp.git reponame.wt.git
cd reponame.wt.git
git config core.bare true
git config --remove-section branch.master
... and I'm not quite there yet ...
But a) it seems too long and complicated, and b) the git repository still thinks it has master
checked out.
Is there a better way to make this "bare clone with remotes"?
(Perhaps there is a better way to set up worktrees in general, but I understand that gets on the hairy edge of the "specific programming solution" scope of stack overflow.)
回答1:
You can configure a bare repository to have remote tracking branches with:
git config --local --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
Run git fetch
and you should see them with git branch -a
.
I don't believe it's possible to not have master
(or another branch) checked out in the repository, but with a bare repository it's safe to create a linked worktree for it anyway, e.g.:
git worktree add --force <path> master
Even though master
is checked out in the bare repository, there's no main worktree for it if the repository is bare, so you don't end up with two worktrees that would get out of sync.
来源:https://stackoverflow.com/questions/38981470/git-clone-create-a-bare-clone-but-with-remote-tracking-branches-for-a-worktre