我无法克隆git仓库。

7

我有一个关于Git的问题,我试图克隆一棵树但没有成功。

git clone https://github.com/cer/event-sourcing-examples/tree/d2077e21aa677a00095f90250470ff011c132ab8/java-spring

我克隆了这个项目。
git clone https://github.com/cer/event-sourcing-examples

我试图切换到那个树,但没有效果。

您有什么建议吗?

最好的问候

5个回答

20

Git无法直接克隆一个树对象。您需要克隆整个存储库,然后检出使用您想要的树对象的提交。但为了减少混淆,请注意“树”和“提交”这两个术语之间的区别:

  • 树是表示目录的 Git 对象,包含到 blob(文件)和其他树对象的链接。 树对象不一定是存储库的根目录。
  • 提交对象包含到存储库的根树的链接以及附加信息,例如提交消息、日期和其他标头。

您只能检出提交对象。少数 Git 命令直接处理树对象(其中包括 git cat-filegit ls-tree)。但是,您的 GitHub URL 中的对象 ID 确实是提交对象的 ID,所以这不是问题。

因此,在克隆存储库后,您可以将所需的提交检出到新分支中:

git checkout -b test-branch d2077e21

如果你要解决的问题只是从远程仓库获取单个提交(或树),那么你可能会失望,因为Git的远程协议不支持这种操作。如果有什么能做的,就是在所需提交的位置将一个分支插入到远程仓库中,然后可以直接克隆该分支,而不需要任何历史记录:

git clone -b test-branch --depth 1 https://github.com/cer/event-sourcing-examples

然而,如果你无法做到这一点,那么你仍然没有运气。远程协议只允许引用命名的引用,而不是任意提交。


我遇到了同样的问题,但只是想回到之前的版本。所以我只需使用 git checkout <treeNumber> 命令切换到之前的版本,然后再执行 git pull origin master 命令即可。 - Budi Mulyo

7

请尝试以下方法,本文使用GIT Bash。

  1. 克隆代码库:

    git clone https://github.com/cer/event-sourcing-examples.git

  2. 进入该目录:

    cd event-sourcing-examples/

  3. 切换分支(我假设你说的“tree”是指分支):

    git checkout wip-vagrant wip-vagrant 是分支名称

  4. 要获取更新,您需要使用 pull 命令:

    git pull

如果您想直接克隆特定分支,请参照上面 Micheal 的评论中的说明。


2
git clone -b <branch> <remote_repo>

例子:

git clone -b my-branch git@github.com:user/myproject.git

替代方案(无需设置公钥):
git clone -b my-branch https://git@github.com/username/myproject.git

我觉得我的问题表述不够清晰。我需要的是通过使用 git clone https://github.com/cer/event-sourcing-examples 克隆树形内容而不是主分支。 - Victor

2
如果您的目标只是获取特定提交的仓库副本......
虽然您不能使用克隆,但可以下载特定提交的仓库zip文件。
此方法适用于GitHub。
此及其他方法可在以下链接中找到: https://coderwall.com/p/xyuoza/git-cloning-specific-commits TL;DR
导航至所需sha的树视图。

https://github.com/<repo_name>/tree/<commit_sha>

下载zip文件。 不要克隆。
Github Tree View

 Open the repo and click the "commits" link 
   (in the bar that says "commits branches packages, etc.)

 Select the commit you want. This will take you to the view showing the changes.
    In the url you will see something like this:
    https://github.com/Colt/webpack-demo-app/commit/eb66c0dc93141080f5b1abb335ec998a1e91d72e

 - Note the sha in the url is preceeded by the word "commit". 
   Replace the word "commit" with the word "tree" to put yourself in the
   tree view. 

 - Finally, click on the green "Clone or download" button
   and Download the ZIP. Don't try to clone.

   This will download the entire repo as it was at that commit.



0
首先,您需要获取完整的存储库并检出存储库以进行commit_sha。
git clone -n <repo_name> 
git checkout <commit_sha>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接