回答

收藏

如何更新或同步 GitHub 上的分叉存储库?

技术问答 技术问答 256 人阅读 | 0 人回复 | 2023-09-11

我分叉了一个项目,应用了几个修复程序,并创建了一个可接受的提取请求。几天后,另一个贡献者做了另一个改变。所以我的叉子不包括这个变化。
4 x) r; ?3 U7 W+ U我怎样才能把零钱放在叉子里?当我有进一步的改变来做出贡献时,是否需要删除和重建我的分叉?还是有更新按钮?
) m% K8 d& Z; Y7 P, I- _( d* q                                                                + ]# p. S7 j4 C) n  k9 ~
    解决方案:                                                                ) l3 }; X" `  M7 r2 Q4 l
                                                                在分叉存储库的本地克隆中,您可以使用原始 GitHub 存储库被添加到远程中。(远程就像存储 URL 的昵称 -origin例如,它是一个。)然后你可以从上游存储库中获得所有的分支,并重新设置你的工作,以继续在上游版本中工作。以下命令可能如下:
3 q, H' k+ d1 T3 g& ]% q, ^
    # 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 branchesgit 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$ \) b1 r# n+ R
假如你不想重写 master 分支的历史记录(比如别人可能已经克隆了),所以你应该用最后一个命令代替git merge upstream/master. 但是,为了进一步发出尽可能干净的拉取请求,最好重新设置基础。! r+ j! V/ V4 b2 a; o" |: W1 I
如果你已经将你的分支重新建立在upstream/master在你的基础上,你可能需要强制把它推给你自己GitHub 分叉存储库。您将这样做:8 Y, }" X/ [6 P9 ]/ \. O4 v
git push -f origin master您只需-f重新定位后第一次使用。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则