Syncing a Forked Git Repo with Upstream Changes
Original Chinese title: git获取fork之后的项目最新代码
The Problem
- When you fork a project on GitHub and work on it locally, updates to the upstream repo aren’t synced automatically—you have to merge them manually.
I hadn’t noticed this before and always assumed forked repos stayed updated on their own. For the longest time I thought every project I forked had simply gone dead.
Solution
Option 1: GUI
-
Change the
Primary remote repositoryURL to the upstream project’s address.Take Master Sanqingshui's nodePPT (an online presentation system written in Node) as an example: after forking it to your GitHub and cloning it locally, when you notice the original project has updates: 1. Change the origin or PR repository URL to the upstream repo. 2. Pull down the latest changes, switch the URL back to your own repo, and push. 3. Done.
Option 2: Command Line
-
Personally, I much prefer this method:
>git remote add <alias> <url> >git fetch <alias> >git merge <alias>/master For example, with NodePPT: >git remote add NodePPT https://github.com/ksky521/nodePPT.git >git fetch NodePPT >git merge NodePPT/master >git commit >git push -
A few handy commands:
git remove -v # View all remote sources so you can pick the right one. git branch -a # View all branches available to merge.
That’s About It.
问题
- github上fork项目之后,在本地coding,如果主项目有更新是不自动推送的。需要手动merge。
之前都没有注意到,还一直以为自己fork的项目都自动更新,很久都仍为自己fork了项目之后这些项目都停了。
解决
方案一 GUI
-
更改下
Primary remote repository地址为源项目的地址这里我以三清水大师的nodePPT 一个用node写的在线ppt系统说明,fork之后代码到自己github,然后clone到本地,发现项目有更新的之后 1. 修改orgin 或者PR repository地址。 2. 然后重新获取下来,在修改回来自己的项目地址,push 3. over
方案二 COMMAND
-
我更钟爱这种方法
>git remote add 别名 地址 >git fetch 别名 >git merge 别名/master 例如NodePPT就是 >git remote add NodePPT https://github.com/ksky521/nodePPT.git >git fetch NodePPT >git merge NodePPT/master >git commit >git push -
几个命令
git remove -v 可以查看所有项目的来源,方便选择。 git branch -a 查看可merge的所有分支。