外面的git checkout分支

eis*_*ati 31 git branch clone git-checkout

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

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

  1. git clone'github-project-url''file-system-folder'
  2. git checkout'indern-branch''file-system-folder'

我确实意识到第二步不是很正确,但我也试图避免使用"cd'file-system-folder'".

Bri*_*ell 66

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

git --git-dir=file-system-folder/.git --work-tree=file-system-folder checkout existing-branch
Run Code Online (Sandbox Code Playgroud)


Gen*_*eck 7

您现在也可以选择-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
Run Code Online (Sandbox Code Playgroud)