外面的git checkout分支

时间:2011-05-20 14:38:36

标签: git branch clone git-checkout

问题:我需要以某种方式检查已在我的文件系统上本地克隆的项目的现有分支,而不是在该项目的特定文件夹中。

解决方案:我正在尝试执行以下操作:

  1. git clone'github-project-url''file-system-folder'
  2. git checkout'indern-branch''file-system-folder'
  3. 我确实意识到第二步不太正确,但我也试图避免使用“cd'file-system-folder'”。

5 个答案:

答案 0 :(得分:62)

您可以使用--git-dir指定要用作存储库的.git目录,并使用--work-tree指定要结帐的工作树。请参阅git man page详情。

git --git-dir=file-system-folder/.git --work-tree=file-system-folder checkout existing-branch

答案 1 :(得分:5)

您现在也可以使用 -C 作为选项。请确保在任何其他命令之前使用它,例如

git -C ~/my-git-repo checkout master

请注意,它不必具体为.git文件夹。这是男子文件:

-C <path>
       Run as if git was started in <path> instead of the current 
       working directory. When multiple -C options are given, each
       subsequent non-absolute -C <path> is interpreted relative to
       the preceding -C <path>.

       This option affects options that expect path name like --git-dir
       and --work-tree in that their interpretations of the path names
       would be made relative to the working directory caused by the -C option.
       For example the following invocations are equivalent:

           git --git-dir=a.git --work-tree=b -C c status
           git --git-dir=c/a.git --work-tree=c/b status

答案 2 :(得分:2)

git clone ./foo ./foo-copy
git --git-dir=./foo-copy/.git --work-tree=./foo-copy checkout branch

答案 3 :(得分:1)

非常欢迎您使用--git-dir--work-tree来避免使用CD,但老实说,只需cd即可。为避免必须回来,您可以在子shell中执行此操作:

git clone foo foo-copy
(cd foo-copy && git checkout branch)

当然,在这种特定情况下,您实际上并不需要两个命令:

git clone -b <branch-to-checkout> foo foo-copy 

答案 4 :(得分:1)

git 2.5添加了使用git worktree拥有多个工作树的功能。所以这种情况下,你会使用像

这样的东西

git worktree add -b new-branch-name ../dir-name existing-branch

然后您可以更改为dir-name并像往常一样进行提交。提交将最终在您的原始存储库中(您使用worktree add)。

当您完成并且您想要的所有内容都已提交时,您可以删除dir-name文件夹并运行git worktree prune以清理回购邮件中的孤立工作树。

相关问题