Fork someone else’s project into your repository and clone it locally. While someone else’s project continues development, this article shows how to synchronize new commits from fork’s project to your own repository.

The core idea is to utilize multiple remote repositories.

Clone code from the clone repository (master branch)

$ git branch
 * masterCopy the code

Keep working directories up to date and clean


$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree cleanCopy the code

Next, take a look at remote warehouses


$ git remote -v
origin	ssh://git@xxx/YOUR_REPO/my-android.git (fetch)
origin	ssh://git@xxx/YOUR_REPO/my-android.git (push)Copy the code

The remote repository location corresponding to the Origin alias points to its own repository (since it was cloned from its own repository)


Next, we will add a point to the target repository (someone else’s repository).

$ git remote add upstream ssh://git@xxx/UPSTREAM_REPO/my-android.gitCopy the code

Then take a look at the remote warehouse


$ git remote -v
origin	ssh://git@xxx/YOUR_REPO/my-android.git (fetch)
origin	ssh://git@xxx/YOUR_REPO/my-android.git (push)
upstream	ssh://git@xxx/UPSTREAM_REPO/my-android.git (fetch)
upstream	ssh://git@xxx/UPSTREAM_REPO/my-android.git (push)Copy the code

Now that the remote repository is associated, use Sourcetree to see the status visually




As you can see, after I fork, there are a lot of commits in their warehouse, and then there are a few commits in my warehouse as well, using rebase to keep in sync.


Master branch of Rebase Upstream

$ git rebase upstream/masterCopy the code

Git self-taught — Rebase Complete Version
After rebase is complete, check that the node is OK



The next step is to perform a push, which is rejected if it goes directly to its own remote repository

$ git push origin master:master
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://git@xxx/YOUR_REPO/my-android.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ... ') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.Copy the code

There are two ways to do this: delete the remote branch and push it directly; Another option is to use git push –force.


Here I choose the second way to push. For the first way, see my other article “Git Self-taught — Rebase Complete edition”.

$ git push -forigin master:master Compressing objects: 100% (186/186), done. Writing objects: 100% (281/281) and 66.82 KiB | 11.14 MiB/s, done. Total 281 (126) delta, reused 184 (41) delta remote: Resolving deltas. 100% (126/126), completed with 54localobjects. To ssh://git@xxx/YOUR_REPO/my-android.git + 8aaa796... d298312 master -> master (forced update)Copy the code

The push is successful and the sourcetree status is OK.




At this point, you synchronize the latest commit from someone else’s repository and keep your own commit.