有没有办法从远程Git回购中只检索一个特定的提交,而不将其克隆到我的PC上?远程回购的结构与我的完全相同,因此不会有任何冲突,但我不知道如何做到这一点,我不想克隆那个巨大的存储库。

我是新来的,有什么办法吗?


当前回答

这样做效果最好:

git fetch origin specific_commit
git checkout -b temp FETCH_HEAD

将“temp”命名为你想要的任何名称…这个分支可能是孤儿

其他回答

我认为'git ls-remote' (http://git-scm.com/docs/git-ls-remote)应该做你想要的。不用用力,取或拉。

最后,我找到了一种方法来克隆特定的提交使用git的樱桃。 假设你在本地没有任何存储库,你要从远程提交特定的文件,

1)在本地和git init中创建空仓库

2) git远程添加url-of-repository

3) git获取原点[这将不会移动您的文件到您的本地工作空间,除非您合并]

4)“输入你需要的”

完成了。这样,您将只有来自特定提交的文件在您的本地。

Enter-long-commit-hash:

你可以使用-> git log——pretty=oneline来获得

# make sure you fetch from the origin first
$ git fetch origin

# view code at COMMIT_ID (abc123)
$ git checkout abc123

# bring only COMMIT_ID (abc123)
# to your branch
# assuming your branch is master
$ git checkout master
$ git cherry-pick abc123

# bring all changes up to 
# COMMIT_ID (abc123) to your branch
# assuming your branch is master
$ git checkout master
$ git merge abc123

参考资料- https://unfuddle.com/stack/tips-tricks/git-pull-specific-commit/

您可以简单地获取远程回购的单个提交

git fetch <repo> <commit-id>

在那里,

<repo>可以是一个远程回购名称(例如origin),甚至是一个远程回购URL(例如https://git.foo.com/myrepo.git) <commit- ID >是提交的ID

例如

git fetch https://git.foo.com/myrepo.git 0a071603d87e0b89738599c160583a19a6d95545

在获取提交(以及丢失的祖先)之后,您可以简单地用它签出

git checkout FETCH_HEAD

注意,这将使你进入“分离的头脑”状态。

我查了一下我的git repo

git pull --rebase <repo> <branch>

允许git为分支拉入所有代码,然后我对我感兴趣的提交进行了重置。

Git reset——hard <commit-hash>

希望这能有所帮助。