Git 更新fork后的本地仓库

第一步:配置remote upstream仓库

  1. 打开Git Bash界面

  2. 查看当前fork仓库的remote仓库配置信息

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

  1. 配置当前fork仓库的upstream仓库地址(即fork的原仓库地址)

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

  1. 查看配置结果

$ 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)

参考文档:Configuring a remote for a fork

第二步:通过fetch拉取更新

  1. 打开Git Bash界面
  2. 通过fetch命令从upstream仓库取得需要的更新,fetch后会被存储在一个本地分支upstream/master上

$ git fetch upstream
remote: Counting objects: 75, done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 62 (delta 27), reused 44 (delta 9)
Unpacking objects: 100% (62/62), done.
From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY> * [new branch] master -> upstream/master

  1. 切换到本地master分支,将upstream/master分支合并到master分支

$ git checkout master

$ git merge upstream/master

**以上三步可以合并为一步:$ git pull upstream master,不过fetch + merge 更可控一点

参考文档:Syncing a fork

第三步:如果需要github上fork的仓库同步更新,则执行git push进行推送

git push origin master

你可能感兴趣的:(Git)