preface

Scenario description: A new project B has been created locally after copying part of project A, and new features have been developed, and project A is also undergoing iterative updates. At this time, the product said to synchronize the functions of project A to project B. Both projects have new updates, so how to synchronize the updates? The idea was to fork and synchronize. The process was a bit convoluted, but the problem was always solvable.

Fork the old project

1. Select create fork in project A;

2. Save the project to a personal warehouse and configure a new project name.

3. There is an option to checkEnable fork syncingDecide whether to follow updates later. You are advised to select this option.

4. Click OK to go to the fork C, and then associate with the local repository.

Analysis: When the project B local repository is associated with the remote repository C just forked, the association is not found. The reason is that project B already has a remote repository.

There are two options: 1. Associate another remote repository with fork’s remote repository C; 2. Modify the remote warehouse corresponding to local warehouse B.

Associate multiple remote repositories

How to associate a remote repository when the default Origin repository already exists? Method 1:

1. The git remote - v / / view the remote warehouse information 2. Git remote add originFork https://git.XXX.com/scm/qhlive/XXX.git (fork project corresponding warehouse address) // Associate the remote repository originForkCopy the code

Method 2: If it is sourceTree, find “Settings” – “Remote Repository” in the upper right corner to add/remove the associated remote repository.

Methods 3:

It is also possible to edit the configuration file directly, as shown below. Git configuration files are the project’s.git files, which can also be found in sourceTree Settings.

Example Modify the remote repository corresponding to the local repository

Method 1:

Git remote - v # check the remote address git remote # check the remote warehouse name git remote set - origin url https://git.XXX.com/xx/xx.git (new address)Copy the code

Method 2: Rename/delete

Git remote rm origin git remote add origin [url]Copy the code

Method 3: Change the configuration file as above.

How does the fork repository update synchronously with the original repository?

Use a diagram to illustrate the fork repository in relation to the original repository

1. Update the upstream warehouse to the local warehouse

Git pull originFork branch_name git pull originFork branch_name git pull originFork branch_name git pull originFork branch_nameCopy the code

Fatal: refusing to merge Suggested historie This is because the two branches are not in a relationship. So what’s the solution? Add — allow-suggested – echo after your action order

  git pull originFork branch_name --allow-unrelated-histories
Copy the code

Tips: If git Merge suggested this problem, also add -allow-suggested -histories.

  git merge originFork branch_name --allow-unrelated-histories
Copy the code

Assume that project B’s origin remote repository and fork’s remote repository both have A branch feature/XXX, you can develop on this branch, commit the local modification, and follow the following steps before each push, then you can realize the synchronous update with upstream original repository A.

  • (1) Synchronize the information of the source warehouse to the local computer
  git remote update feature/XXX
Copy the code
  • (2) Merge source repository information into local branch:
  git checkout feature/XXX
  git rebase source_repository_name/feature/XXX
Copy the code
  • (3) Git push will submit the latest synchronized code and modification to your Origin repository

  • (4) Put forward the Push Request and submit the modification to the remote warehouse of project A