I was encountered into this situation that I need to transfer some existing code to another separated GitHub repository. Ideally it would be perfect to keep all historic git commit histories so that we can still be able to revert back if necessary.
After digging into Google and Stack Overflow, luckily I found there are others who need to achieve similar things.
Here
is the solution I used to resolve this problem eventually.
Example Operations#
Below are my git operations when transfer the code from existing repository to a new GitHub repository ABC/common.git
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| 4690 2020-01-29 15:44 git filter-branch --subdirectory-filter common -- -- all
4691 2020-01-29 15:44 ls
4692 2020-01-29 15:44 cd loyalty
4693 2020-01-29 15:44 ls
4694 2020-01-29 15:44 ls | wc
4695 2020-01-29 15:44 ls | wc -l
4696 2020-01-29 15:44 cd ..
4697 2020-01-29 15:44 ls
4698 2020-01-29 15:45 mkdir common
4699 2020-01-29 15:45 git mv README.md athena_glue.py dag_common deploy.sh member_match.py s3_utils.py slack.py common
4700 2020-01-29 15:45 ls
4701 2020-01-29 15:45 git mv loyalty/ common
4702 2020-01-29 15:45 git mv schemas/ common
4703 2020-01-29 15:45 ls
4704 2020-01-29 15:45 git mv __init__.py common
4705 2020-01-29 15:45 ls
4706 2020-01-29 15:45 git commit -m "relocate common library to ABC/common repo"
4707 2020-01-29 15:45 cd ..
4708 2020-01-29 15:45 ls
4709 2020-01-29 15:46 cd common
4710 2020-01-29 15:46 git remote add dp ../dp/
4711 2020-01-29 15:46 git fetch dp
4712 2020-01-29 15:46 git status
4713 2020-01-29 15:48 git branch dp remotes/dp/master
4714 2020-01-29 15:49 git merge dp --allow-unrelated-history
4715 2020-01-29 15:49 git merge dp --allow-unrelated-histories
4716 2020-01-29 15:49 git remote rm dp
4717 2020-01-29 15:49 git branch list
4718 2020-01-29 15:49 git branch
4719 2020-01-29 15:49 git status
4720 2020-01-29 15:50 idea .
4721 2020-01-29 15:51 cd ..
4722 2020-01-29 15:51 ls
4723 2020-01-29 15:51 rm -rf common
4724 2020-01-29 15:51 git clone [email protected]:ABC/common.git
4725 2020-01-29 15:51 cd common
4726 2020-01-29 15:51 idea .
4727 2020-01-29 15:54 git remote add dp ../dp
4728 2020-01-29 15:54 git fetch dp
4729 2020-01-29 15:55 git branch dp remotes/dp/master
4730 2020-01-29 15:55 git merge dp --allow-unrelated-histories
|
Hope it helps.