前言
在 github 上 一般我们都是将别人的 repo fork 到自己的 github repo。 然后阅读,修改 甚至是 contribution。所以同步源 repo 的 commit 尤为重要。因为你要始终保持与源 repo 同步。下面将描述下怎么去同步更新的步骤。
添加 upstream
将源 repo https link 加到自己本地 repo 的 remote upstream 中。添加之后,如果你想查看是否加入成功。可以使用 git remote -v
git remote add upstream https://github.com/ORIGINAL_OWENER/ORIGINAL_REPO.git
fetch 源 repo
fetch 源 repo 的最新提交到本地,然后 merge 到自己的 repo
git fetch upstream
git rebase upstream/branch
合并并提交
将自己的 repo 合并后,提交到 github 上
git push origin branch
Note: 有些时候,我们会遇到push失败的问题,我们将需要强制提交,从而覆盖 github 上的 commit, 此命令慎用
git push -f origin branch
总结
在合作开发的时候,还是时不时的 rebase 下是最好的,避免不必要的冲突麻烦。