Merge two git repositories

From: Merge two git repositories

 

Premise and expectations:

  1. There are 2 git repositories: repo1, repo2;
  2. Want to move the files in repo1 into repo2;
  3. The historical log of repo1 should be kept;
First, quickly create 2 repositories.
1
2
3
4
5
6
7
8
9
10
11
12
13
mkdir repo1
cd repo1
git init
echo "repo1.txt" > repo1.txt
git add repo1.txt
git ci -m "init repo1"
 
mkdir repo2
cd repo2
git init
echo "repo2.txt" > repo2.txt
git add repo2.txt
git ci -m "init repo2"

The resulting directory path is:

1
2
3
4
5
6
7
repo1/
repo1/repo1.txt
repo1/.git
 
repo2/
repo2/repo2.txt
repo2/.git

Note again that the desired effect is:

1
2
3
4
repo2/
repo2/repo1.txt
repo2/repo2.txt
repo2/.git
Then, a five-step command is required:
1
2
# 1. Add repo1 as a remote repository, add it to repo2, and set the alias to other 
[jot@myhost repo2]$ git remote add other ../repo1/

 

 

1
2
3
4
5
6
7
8
# 2. Grab data from repo1 warehouse to this warehouse
[jot@myhost repo2]$ git fetch other
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../repo1
 * [new branch]      master     -> other/master

 

 

1
2
3
4
# 3. Checkout the master branch captured by the repo1 warehouse as a new branch to the local, and set the new branch name to repo1
[jot@myhost repo2]$ git checkout -b repo1 other/master
Branch repo1 set up to track remote branch master from other.
Switched to a new branch 'repo1'

 

 

1
2
3
# 4. Switch back to the master branch of repo2
[jot@myhost repo2]$ git checkout master
Switched to branch 'master'

 

 

1
2
3
4
5
6
# 5. Merge repo1 into the master branch
[jot@myhost repo2]$ git merge repo1
Merge made by recursive.
 repo1.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 repo1.txt
You can see the effect, the log is still there:
It has been completed and can be pushed to the server.
Possible problems:
1. When merging, it is possible that two branches have modified the same file. At this time, the conflict needs to be resolved. For text files, it is very simple. You can process the conflicting position as needed. For binary files, you need to use the following commands.
git checkout --theirs YOUR_BINARY_FILES // keep changes from branches that need to be merged in
//git checkout --ours YOUR_BINARY_FILES // keep your changes
git add YOUR_BINARY_FILES
git comm
Summarize:
    1. 大致思路是伪造远程的repo1仓库为repo2的一个分支,然后合并进来;
    2. 若是文件有冲突、或要建立子目录,建议在repo1中先解决,再进行如上操作。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324969835&siteId=291194637