git fork后更新原作者仓库代码到自己仓库

BG7ZAG

分类: 程序开发 1636 0

fork 了别人的仓库后,原作者又更新了仓库,如何将自己的代码和原仓库保持一致?本文将给你解答。

假设远程源仓库为A,自己fork后的远程仓库为B,自己本地的代码仓库为C

给 fork 配置一个 remote

一般来说从自己远程仓库B去拉代码后就会有remote

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

git remote -v
origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)

添加一个将被同步给 fork 远程的上游仓库A

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

再次查看状态确认是否配置成功。

git remote -v
origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

执行同步fork操作

从上游仓库A fetch 分支和提交点,传送到本地,并会被存储在一个本地分支 upstream/master

git fetch upstream

默认会将远程所有的分支fetch下来

remote: Counting objects: 41, done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 41 (delta 17), reused 0 (delta 0)
Unpacking objects: 100% (41/41), done.
From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
 * [new branch]      dev        -> upstream/dev
 * [new branch]      master     -> upstream/master

将upstream的代码合并到本地仓库C上

同步自己的远程仓库

从自己的远程仓库B上拉取最新的代码

git pull origin master

执行合并upstream操作

把 upstream/master 分支合并到本地 master 上

git merge upstream/master

如果想同步upstream/dev 分支的话执行

git merge upstream/dev

把 upstream/dev 分支合并到本地

push本地代码到自己的远程仓库

处理冲突代码,后提交到自己的远程仓库

git add .
git commit -m '描述'
git push

文章来自:shunyang

  • 0人 Love
  • 1人 Haha
  • 0人 Wow
  • 0人 Sad
  • 0人 Angry
git、git fork、github

作者简介: BG7ZAG

打赏

博客站长,前端开发工程师

共 0 条评论关于 “git fork后更新原作者仓库代码到自己仓库”

Loading...