git checkout 从外部分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6073507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:30:34  来源:igfitidea点击:

git checkout branch from outside

gitbranchclonegit-checkout

提问by eistrati

Problem: I need somehow to checkout an existing branch of a project that is already cloned locally on my file system without being in that particular folder of this project.

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

Solution: I'm trying to do the following:

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

  1. git clone 'github-project-url' 'file-system-folder'
  2. git checkout 'existing-branch' 'file-system-folder'
  1. git clone 'github-project-url' '文件系统-文件夹'
  2. git checkout '现有分支' '文件系统文件夹'

I do realize that second step is not quite right, but I also am trying to avoid to "cd 'file-system-folder'".

我确实意识到第二步不太正确,但我也试图避免“cd 'file-system-folder'”。

回答by Brian Campbell

You can use --git-dirto specify the .gitdirectory to use as the repository, and --work-treeto specify the working tree to to the checkout in. See the gitman pagefor details.

您可以使用--git-dir指定.git要用作存储库的目录,并--work-tree指定要检入的工作树。有关详细信息,请参阅git手册页

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

回答by General Redneck

Since Git version 1.8.5, you can also use -C <path>option. Be sure to use it before any other command:

从 Git 版本1.8.5 开始,您还可以使用-C <path>选项。确保在任何其他命令之前使用它:

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

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

Note that it doesn't have to be specifically the .git folder. Here is the man documenation:

请注意,它不必专门是 .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
-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

回答by Robin Green

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

回答by Cascabel

You're quite welcome to use --git-dirand --work-treeto avoid cd'ing, but honestly, it's easier just to cd. To avoid having to cd back, you can do it in a subshell:

非常欢迎您使用--git-dir--work-tree避免 cd'ing,但老实说,仅使用 cd 更容易。为避免 cd 返回,您可以在子 shell 中执行此操作:

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

Of course, in this specific case, you don't actually need two commands:

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

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

回答by Wilka

git 2.5 added the ability to have multiple working trees using git worktree. So this case, you'd use something like

git 2.5 添加了使用git worktree拥有多个工作树的能力。所以在这种情况下,你会使用类似的东西

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

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

you can then change to dir-name and make your commits as usual. The commits will end up in your original repository (where you used worktree add).

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

When you're done and everything you want is committed, you can delete the dir-namefolder and run git worktree pruneto clean up the orphaned worktree in your repo.

当您完成并提交您想要的所有内容后,您可以删除该dir-name文件夹并运行git worktree prune以清理您的存储库中的孤立工作树。