git:从外部检查工作树分支

git: checkout work-tree branch from outside

Git版本:2.5.1.windows.1(通过git-scm.com)

设置:

Main Repo:    Branch_A -> commit -> commit -> commit -> commit
                             \
Worktree:                     -> Branch_B -> commit -> commit
                                     \
                                      -> local Branch_C -> commit

我通过

创建了第二个工作树
git worktree add /C/repos/testbranch Branch_B

效果很好

现在我要做的是在工作树中创建本地分支(如 Branch_C)并提交给它们后,将工作树的分支从外部切换回分支 Branch_B。 在 this 之后,我尝试了以下操作:

签出的分支是 Branch_C,在 Branch_B

上有一个额外的提交
git --work-tree=/C/repos/testbranch/ --git-dir=/C/repos/testrepo/.git/ checkout Branch_B

但我得到的输出是:

error: Your local changes to the following files would be overwritten by checkout:
    bla <-- My test file which is COMMITTED and UNCHANGED in Branch_C
Please, commit your changes or stash them before you can switch branches.
Aborting

我尝试从不同的目录执行此操作,尝试了 adding/omitting --git-dir 和其他一些东西 - 似乎没有任何效果。我做错了什么?

编辑:通过

签出分支
git checkout Branch_B

在 /C/repos/testbranch/ 中工作正常。


背景:我使用的存储库是从 SVN 存储库克隆而来的。我想要实现的是使用 ANT( 任务)自动执行 "svn rebase"ing 的过程。第一步是检出指向 SVN 存储库上的分支的分支。不幸的是,仅在 ANT 任务中使用 checkout 失败并显示 not a git repository: $SECOND_WORKTREE_DIR.

步骤:

  1. 结帐$orig_branch
  2. git svn fetch
  3. (可选)git svn find-rev 最新版本的修订版(可能不是最后一次提交)
  4. git 变基 $svn-branch_build-rev $orig_branch

找到解决方案。

  1. 您无需在工作树的文件夹中工作,因为存储库保持不变。假设您需要执行 SVN 提取,您可以从主工作树中执行。
  2. 您可以通过执行与git一起安装的sh.exe并向其传递命令(也可以是cd)来规避上述问题:





Git 2.11+(2016 年第 4 季度)可能会更优雅地处理这种情况:

commit 112423e, commit b26098f (14 Oct 2016) by Eric Wong (ele828)
(由 Junio C Hamano -- gitster -- in commit 251641b 合并,2016 年 10 月 27 日)

git-svn: "git worktree" awareness

git-svn internals were previously not aware of repository layout differences for users of the "git worktree" command.

Introduce this awareness by using "git rev-parse --git-path" instead of relying on outdated uses of GIT_DIR and friends.