Warm tip: This article is reproduced from serverfault.com, please click

git-如何在GitHub上更新或同步分支的存储库?

(git - How do I update or sync a forked repository on GitHub?)

发布于 2011-08-30 13:53:00

我分叉了一个项目,应用了多个修复程序,并创建了一个接受请求。几天后,另一位贡献者进行了另一项更改。因此,我的前叉不包含该更改。

我怎样才能把零钱放到我的叉子上?当我有其他更改要贡献时,是否需要删除并重新创建我的派生叉?还是有一个更新按钮?

Questioner
Lea Hayes
Viewed
0
2020-11-24 18:50:01

在你的分支存储库的本地克隆中,你可以将原始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

如果你不想重写master分支的历史记录(例如,因为其他人可能已经克隆了它),则应使用替换最后一个命令git merge upstream/master但是,为了发出更多尽可能干净的拉取请求,最好重新设置基准。


如果你已将分支重新建立基础upstream/master,则可能需要强制执行推送才能将其推送到GitHub上你自己的分叉存储库中。你可以这样做:

git push -f origin master

-f重新设定基准后,你只需要在第一时间使用