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

How can I clone a branch with the same folder name as the branch?

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

的文件夹

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

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

I tried to clone different branches in the same location

我认为您不应该这样做,因为它在您的环境中引入了太多活动部件。为什么不正常 克隆 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 是将 checkout 的分支。
  • 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