我正在使用一个代码库,在这里,我需要同时处理几个分支,以实现不同的目的。因此,我克隆到一个裸存储库,然后设置一些工作树:
git clone --bare ssh://git@git.example.com/project/repo repo.git
cd repo.git
git worktree add ../branch-1 branch-1
git worktree add ../branch-2 branch-2
... someone else creates branch-3 and pushes is ...
git fetch origin +refs/heads/*:refs/heads/* --prune
git worktree add ../branch-3 branch-3现在,branch-3工作树并没有被设置为跟踪远程树,并试图让它这样做,我陷入了可怕的混乱。
$ cd ../branch-3
$ git branch -u origin/branch-3
error: the requested upstream branch 'origin/refs/heads/feature/SW-5884-move-database-container-to-alpine-base-2' does not exist
hint: ...<snip>
$ git fetch +refs/heads/*:refs/remotes/origin/* --prune
$ git branch -u origin/branch-3
fatal: Cannot setup tracking information; starting point 'origin/feature/SW-5884-move-database-container-to-alpine-base-2' is not a branch.什么才能让这件事奏效呢?
发布于 2021-08-29 11:35:09
我认为有一种非常简单的方法;
1.克隆无结账的回购(无浪费空间)
git clone --no-checkout ssh://git@git.example.com/project/repo repo.git2.去回购
cd repo.git3.创建一个虚拟分支,以便在现有的每个分支上创建一个工作树
git switch -c dummy4.现在根据需要创建工作树
git worktree add branch-1或
git worktree add pathtobr1 branch-1就这样。它干净,容易,不浪费空间。
希望这有帮助;-)
发布于 2020-06-22 22:53:47
我与此斗争了很长一段时间,从来不记得我所采取的步骤,以创建一个裸露的回购,不像一个。最后,我编写了名为git-clone-bare-for-worktrees的脚本,用于创建用于工作树的裸repos。它似乎运行得相当好,但请注意,它并没有做太多的错误处理。
#! /bin/env bash
set -e
url=$1
name=${url##*/}
git init --bare "${name}"
cd "${name}"
git config remote.origin.url "$url"
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
git fetch
firstCommit=$(git rev-list --all --max-parents=0 --date-order --reverse | head -n1)
git branch bare-dummy $firstCommit
git symbolic-ref HEAD refs/heads/bare-dummy它创建一个名为bare-dummy的分支,指向回购中的第一个提交,并将其设置为HEAD,确保所有“真实”分支都可用,以便在工作树中安全地签出。除此之外,回购不会包含任何本地分支,甚至不包含master,但远程跟踪分支的创建将与正常的非裸克隆完全相同。所以,只要运行一个快速的git worktree add ../master-worktree master,您就可以启动并运行。
发布于 2022-05-18 13:05:30
另一种解决方案是在克隆时使用--separate-git-dir参数:
$ mkdir project && cd project
$ git clone http://****/wt.git main --separate-git-dir=.git
Cloning into 'main'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
$ git worktree add wt1 b1
Preparing worktree (new branch 'b1')
branch 'b1' set up to track 'origin/b1'.
HEAD is now at 23b2efc Added file
$ ls -a
. .. .git main wt1这种技术的主要缺点是,如果尝试运行git命令,根project目录的行为就像一个不同步的工作树:
project$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
main/
wt1/
no changes added to commit (use "git add" and/or "git commit -a")但其他一切看上去都与预期完全一致。
在我看来,这是所有答案中最好的妥协。
https://stackoverflow.com/questions/54367011
复制相似问题