我有一个关于Git的问题,我试图克隆一棵树但没有成功。
git clone https://github.com/cer/event-sourcing-examples/tree/d2077e21aa677a00095f90250470ff011c132ab8/java-spring
我克隆了这个项目。
git clone https://github.com/cer/event-sourcing-examples
我试图切换到那个树,但没有效果。
您有什么建议吗?
最好的问候
Git无法直接克隆一个树对象。您需要克隆整个存储库,然后检出使用您想要的树对象的提交。但为了减少混淆,请注意“树”和“提交”这两个术语之间的区别:
您只能检出提交对象。少数 Git 命令直接处理树对象(其中包括 git cat-file
和 git 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 Bash。
克隆代码库:
git clone https://github.com/cer/event-sourcing-examples.git
进入该目录:
cd event-sourcing-examples/
切换分支(我假设你说的“tree”是指分支):
git checkout wip-vagrant
wip-vagrant 是分支名称
要获取更新,您需要使用 pull 命令:
git pull
如果您想直接克隆特定分支,请参照上面 Micheal 的评论中的说明。
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
https://github.com/<repo_name>/tree/<commit_sha>
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.
git clone -n <repo_name>
git checkout <commit_sha>
git checkout <treeNumber>
命令切换到之前的版本,然后再执行git pull origin master
命令即可。 - Budi Mulyo