preface

When we use GitHub and see a good project or want to contribute to a project, we often fork the repository to our own account. If the source repository code changes during this time, it needs to be synchronized with the source repository code. This article will show you how to implement this operation.

Configure the upstream repository for the project

First, clone the repository code of fork to the local repository. All subsequent operations are based on the local repository. For example, you can download the fork code locally with Git Clone:

git clone [email protected]:secbr/nacos.git
Copy the code

The subsequent steps are based on the local repository.

Go to the local warehouse directory

Enter the root directory of the clone local warehouse through CD operation:

cd /Users/apple/develop/nacos-request/nacos
Copy the code

Subsequent operations are performed in the directory of the local warehouse without special instructions.

View the remote repository path

Run the git remote -v command to check the path of the remote repository.

appledeMacBook-Pro-2:nacos apple$ git remote -v
origin  https://github.com/secbr/nacos.git (fetch)
origin  https://github.com/secbr/nacos.git (push)
Copy the code

If only 2 lines are displayed, the project has not yet set upstreams (upstream). In general, setting upstreams once does not need to be repeated. By comparing the remote repository path with the clone path, it can be found that the remote repository path is still the fork project path.

Add upstream

Git remote add upstream xxx.git set the fork repository to upstream. The project here is fork from Alibaba’s NacOS warehouse, so the corresponding upstream is the address of Alibaba’s source warehouse.

Run the git remote -v command to check whether the command is successful.

appledeMacBook-Pro-2:nacos apple$ git remote add upstream https://github.com/alibaba/nacos.git
appledeMacBook-Pro-2:nacos apple$ git remote -v
origin  https://github.com/secbr/nacos.git (fetch)
origin  https://github.com/secbr/nacos.git (push)
upstream        https://github.com/alibaba/nacos.git (fetch)
upstream        https://github.com/alibaba/nacos.git (push)
Copy the code

Upstream was added successfully. Upstream was added successfully.

Check local code status

Since our example is cloned directly from the repository, no code changes have been made locally. If your local project has changed some code and you are not sure if it has been committed, you need to check with Git status.

appledeMacBook-Pro-2:nacos apple$ git status
On branch develop
Your branch is up to date with 'origin/develop'.

nothing to commit, working tree clean
Copy the code

As shown above, there is no local code to commit. If changes are made locally, you need to push them from the local repository to the GitHub repository first. Then, perform another Git status check.

The basic procedure for pushing to the GitHub repository is as follows:

Git add -a or git add filename git commit -m "your note" git push origin master git statusCopy the code

After completing the basic operations above and verifying that the code has been committed, you are ready to merge the source and local repositories.

Fetch updates to the source repository

After the preparation of the above steps, we are ready to make code updates to the source repository. Git fetch upstream:

appledeMacBook-Pro-2:nacos apple$ git fetch upstream remote: Enumerating objects: 2646, done. remote: Counting objects: 100% (2593/2593), done. remote: Compressing objects: 100% (1157/1157), done. remote: Total 2646 (delta 731), reused 2404 (delta 682), pack-reused 53 Receiving objects: 100% (2646/2646) and 1.67 MiB | 1.47 MiB/s, done. Resolving deltas: 100% (734/734), Completed with 37 local objects. The From https://github.com/alibaba/nacos * [new branch] 0.2.1 - > upstream / 0.2.1 * [new Branch] 0.2.2 - > upstream / 0.2.2 * [new branch] 0.3.0 - > upstream / 0.3.0 / /... Omit partCopy the code

After executing the above command, the update (COMMIT) to the upstream repository will be a local branch, usually named upstream/BRANCHNAME. Upstream /0.3.0, for example.

Switch branch

After pulling the upstream warehouse branch, check which branch the local warehouse is currently in, that is, the branch that needs to be updated and merged. For example, here I need to update the contents of the Develop branch to match the upstream repository code. Switch to the Develop branch first:

appledeMacBook-Pro-2:nacos apple$ git checkout develop
Already on 'develop'
Your branch is up to date with 'origin/develop'.
Copy the code

This is the Develop branch.

Execute mergers

Run git merge upstream/develop to merge remote develop branches. For example, if you merge a master, replace the Develop name with the corresponding master if necessary.

appledeMacBook-Pro-2:nacos apple$ git merge upstream/develop Removing test/src/test/java/com/alibaba/nacos/test/naming/DeregisterInstance_ITCase.java // ... Omit part of o naming/SRC/test/Java/com/alibaba/nacos/naming/core/PushServiceTest. Java Auto - done client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java CONFLICT (content): Merge conflict in client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java Removing client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java Removing .editorconfig Automatic merge failed; fix conflicts and then commit the result.Copy the code

After executing the above command, you will see that changes to the branch specified by the upstream code are reflected in the local code.

Upload code to the fork branch

After the previous merge, there is some follow-up, such as code conflicts. Conflicts can occur if the content is modified locally and the upstream repository also modifies the corresponding code. This is where you need to compare your code to make changes. I am more used to using visual plug-ins in IDEA for code conflict resolution, you can also choose your favorite way to resolve.

Once the conflict is resolved, you can perform the normal code add, COMMIT, and push operations. Here are a series of operations for the repository of its own fork, the corresponding operation example is as follows:

appledeMacBook-Pro-2:nacos apple$ git add . appledeMacBook-Pro-2:nacos apple$ git commit -m 'merge from nacos' [develop 8601c1791] merge from nacos appledeMacBook-Pro-2:nacos apple$ git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 281.00 100% (2/2), 281 bytes | KiB/s, done. Total 2 (1) delta, reused zero (0) delta, pack - reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/secbr/nacos.git 76a4dcbb1.. 8601c1791 develop -> developCopy the code

The above actions commit the changes from the source repository to the fork through the add, commit, and push operations. At this point, I look at my forked GitHub repository and see that the code has been updated.

summary

This article shows you how to synchronize the latest code from the upstream repository with your own repository when we fork a repository. If you like to fork good code and contribute to open source projects on GitHub, this basic practice is a must.

About the blogger: Author of the technology book SpringBoot Inside Technology, loves to delve into technology and writes technical articles.

Public account: “program new vision”, the blogger’s public account, welcome to follow ~

Technical exchange: Please contact the weibo user at Zhuan2quan