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

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


当前回答

如果请求的提交是在远程回购的pull请求中,你可以通过它的ID获得它:

# Add the remote repo path, let's call it 'upstream':
git remote add upstream https://github.com/repo/project.git

# checkout the pull ID, for example ID '60':
git fetch upstream pull/60/head && git checkout FETCH_HEAD

其他回答

你可以简单地获取远程回购:

git fetch <repo>

在那里,

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

例如:

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

在你获得repo之后,你可以合并你想要的提交(因为这个问题是关于检索一个提交,而不是合并,你可以使用cherry-pick来选择一个提交):

git merge <commit>

<commit>可以是SHA1提交

例如:

git cherry-pick 0a071603d87e0b89738599c160583a19a6d95545

or

git merge 0a071603d87e0b89738599c160583a19a6d95545

如果是你想合并的最新提交,你也可以使用FETCH_HEAD变量:

git cherry-pick (or merge) FETCH_HEAD

我查了一下我的git repo

git pull --rebase <repo> <branch>

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

Git reset——hard <commit-hash>

希望这能有所帮助。

# 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 reset --hard <commitID>

如果请求的提交是在远程回购的pull请求中,你可以通过它的ID获得它:

# Add the remote repo path, let's call it 'upstream':
git remote add upstream https://github.com/repo/project.git

# checkout the pull ID, for example ID '60':
git fetch upstream pull/60/head && git checkout FETCH_HEAD