【问题标题】:How can I clone a branch with the same folder name as the branch?如何克隆与分支具有相同文件夹名称的分支?
【发布时间】:2021-08-19 07:19:15
【问题描述】:

我试图在同一位置克隆不同的分支,但它不断创建名称为 master(main) 而不是特定的 branch 的文件夹。

我使用的命令: git clone --branch <branch-name> <git-URL>/sampleproject.git

我本地驱动器上的文件夹名称是sampleproject。如何克隆文件夹名称为 <branch-name> 的分支?

【问题讨论】:

  • 在克隆命令的末尾添加
  • “克隆文件夹”是什么意思? Git 不是 SVN,分支也不是“文件夹”。在 git 中,将分支视为具有自己的时间线的平行宇宙,它可能会或可能不会与另一个分支/宇宙/时间线融合。
  • git clone<directory> 参数是最后一个命令行参数,所以git clone --branch sampleproject <git-URL>/sampleproject.git sampleproject 应该这样做。
  • 文件夹名称与克隆时签出的分支无关。
  • 注意你不是在克隆一个分支。您正在下载一个完整的克隆,然后签出一个特定的分支。

标签: git github bitbucket cloning


【解决方案1】:

我尝试在同一位置克隆不同的分支

我认为您不应该这样做,因为它在您的环境中引入了太多移动部件。为什么不克隆 repo 正常然后git checkout你想要的分支?

例如

cd ~/repos

# Clone the repo into the directory `~/repos/sampleproject` (which will default to checking out the 'master' or 'main' branch):
git clone <git-URL>/sampleproject.git sampleproject

# Get into the repo:
cd sampleproject

# Checkout the branch 'sampleproject'
git checkout -b sampleproject

更简洁地说,git clone 命令可以让您在一行中执行此操作:

git clone --branch sampleproject <git-URL>/sampleproject.git sampleproject

请注意,名称sampleproject 重复了 3 次,因为:

  • --branch sampleprojectcheckout'd 的分支。
  • sampleproject.git 是要克隆的远程仓库。
  • sampleproject(在末尾)是您本地文件系统中将要克隆 repo 的目录的名称。

但这是一个坏主意:不要因为不同的事物使用相同的名称而混淆自己。如果这是我的仓库,我会将 sampleproject 分支重命名为更具描述性的名称。


但不是每次都从远程仓库克隆它,why not use git's support for concurrent working-trees?

cd ~/repos

# Clone the repo into the directory `~/repos/sampleproject-master` (which will default to checking out the 'master' or 'main' branch):
git clone <git-URL>/sampleproject.git sampleproject-master

# Checkout the `branch1` branch to the directory `~/repos/sampleproject-branch1`
git worktree add --checkout -b branch1 sampleproject-branch1

# Checkout the `branch2` branch to the directory `~/repos/sampleproject-branch2`
git worktree add --checkout -b branch2 sampleproject-branch2

【讨论】:

  • 你忘了 cd 进入 repo?
  • @matt 在哪里?在我的第一个示例中,cd 是第三个命令。在我的worktree 的另一个示例中,您想将cd 放入repo 目录。工作树应该分开。
猜你喜欢
  • 2022-01-20
  • 2021-12-21
  • 2021-06-26
  • 2013-11-25
  • 2022-06-17
  • 2011-05-15
  • 2021-10-01
  • 2017-05-05
  • 1970-01-01
相关资源
最近更新 更多