fork 了别人的仓库后如何保持同步更新

惊天一问:fork 了别人的仓库后,原作者又更新了仓库,如何将自己的代码和原仓库保持一致呢?

1 . Configuring a remote for a fork

  • 给 fork 配置一个 remote

  • 主要使用 git remote -v 查看远程状态。

git remote -v
# origin  https://github.com/shengjk/flink.git (fetch)
# origin  https://github.com/shengjk/flink.git (push)
  • 添加一个将被同步给 fork 远程的上游仓库
git remote add upstream https://github.com/apache/flink.git 
  • 再次查看状态确认是否配置成功。
git remote -v
origin  https://github.com/shengjk/flink.git (fetch)
origin  https://github.com/shengjk/flink.git (push)
upstream        https://github.com/apache/flink.git (fetch)
upstream        https://github.com/apache/flink.git (push)

2. Syncing a fork

  • 从上游仓库 fetch 分支和提交点,传送到本地,并会被存储在一个本地分支 upstream/master
    git fetch upstream
git fetch upstream
remote: Enumerating objects: 192, done.
remote: Counting objects: 100% (192/192), done.
remote: Compressing objects: 100% (71/71), done.
remote: Total 232 (delta 61), reused 189 (delta 60), pack-reused 40
Receiving objects: 100% (232/232), 42.32 KiB | 147.00 KiB/s, done.
Resolving deltas: 100% (61/61), completed with 24 local objects.
From https://github.com/apache/flink
   0ab1549f52..3cbaabc527  master      -> upstream/master
   de3772bfd2..1b9c464ab3  release-1.6 -> upstream/release-1.6
   c193cf04a4..8012ab17bc  release-1.7 -> upstream/release-1.7

  • 切换到本地主分支
    git checkout master
git checkout master
# Switched to branch 'master'
  • 把 upstream/master 分支合并到本地 master 上,这样就完成了同步,并且不会丢掉本地修改的内容。
    git merge upstream/master
git merge upstream/master

  • 如果想更新到 GitHub 的 fork 上,直接 git push origin master 就好了。

这里推荐使用rebase,因为merge之后,会有记录,然后在提交PR,会很难看

最重要的一条:

**年前最后一更,提前祝大家新年快乐!✌️?**

你可能感兴趣的:(工作之行)