如何克隆与分支具有相同文件夹名称的分支?

时间:2021-06-01 05:02:56

标签: git github bitbucket cloning

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

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

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

1 个答案:

答案 0 :(得分:0)

<块引用>

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

我认为您不应该这样做,因为它在您的环境中引入了太多活动部件。为什么不正常克隆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 sampleproject 是将要checkoutd 的分支。
  • sampleproject.git 是将被克隆的远程存储库。
  • sampleproject(最后)是本地文件系统中存储库将被克隆到的目录的名称。

但这是个坏主意:不要为不同的事物使用相同的名称而混淆自己。如果这是我的存储库,我会将 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
相关问题