我分叉了一个项目,进行了更改,并创建了一个被接受的拉取请求。新的提交后来被添加到存储库中。我怎样才能把这些承诺交给我?


当前回答

如何在本地计算机上更新分叉回购?

首先,检查遥控器/主机

git remote -v

你应该有起点和上游。例如:

origin  https://github.com/your___name/kredis.git (fetch)
origin  https://github.com/your___name/kredis.git (push)
upstream    https://github.com/rails/kredis.git (fetch)
upstream    https://github.com/rails/kredis.git (push)

之后转到main:

git checkout main

并从上游合并到干管:

git merge upstream/main

其他回答

实际上,可以从浏览器中上游的任何提交在fork中创建分支:

打开https://github.com/<repo>/commits/<hash>,其中repo是您的分叉,hash是提交的完整哈希,您可以在上游web界面中找到。例如,我可以打开https://github.com/max630/linux/commits/0aa0313f9d576affd7747cc3f179feb097d28990,这将linux master作为编写时间。单击“树:……”按钮。键入新分支的名称,然后按Enter键

然后,您可以将该分支提取到本地克隆,当您在提交的基础上进行编辑时,您不必将所有数据推回到GitHub。或者使用web界面更改该分支中的某些内容。

它是如何工作的(这是一个猜测,我不知道GitHub是如何做到的):forks共享对象存储,并使用命名空间来分隔用户的引用。因此,您可以通过fork访问所有提交,即使在分叉时它们还不存在。

克隆分叉存储库后,转到克隆所在的目录路径和GitBash终端中的几行。

$ cd project-name

$ git remote add upstream https://github.com/user-name/project-name.git
 # Adding the upstream -> the main repo with which you wanna sync

$ git remote -v # you will see the upstream here 

$ git checkout master # see if you are already on master branch

$ git fetch upstream

你很好去那里。主存储库中所有更新的更改都将被推送到您的fork存储库中。

“fetch”命令对于保持项目中的最新状态是必不可少的:只有在执行“gitfetch”时,您才会被告知同事对远程服务器所做的更改。

您仍然可以访问此处进行进一步查询

我用这一行更新我的分叉回购:

git pull https://github.com/forkuser/forkedrepo.git branch

如果您不想在项目中添加另一个远程端点,请使用此选项,如其他解决方案所示。

在分叉存储库的本地克隆中,可以将原始GitHub存储库添加为“远程”。(“Remotes”就像是存储库URL的昵称,例如,origin就是其中之一。)然后,您可以从上游存储库中获取所有分支,并重新设置工作基础,继续使用上游版本。就命令而言,可能如下所示:

# Add the remote, call it "upstream":

git remote add upstream https://github.com/whoever/whatever.git

# Fetch all the branches of that remote into remote-tracking branches

git fetch upstream

# Make sure that you're on your master branch:

git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:

git rebase upstream/master

如果您不想重写主分支的历史记录(例如,因为其他人可能已经克隆了它),那么应该用gitmergeupstream/master替换最后一个命令。然而,为了做出尽可能干净的进一步拉取请求,可能最好重新设置基址。


如果您已经将分支重新基于upstream/master,则可能需要强制推送,以便将其推送到GitHub上自己的分叉存储库。你可以这样做:

git push -f origin master

您只需要在重新启动后第一次使用-f即可。

rm -rf oldrepository
git clone ...

可能有更微妙的选项,但这是我唯一有信心的方法,我的本地存储库与上游存储库相同。