Merging 2 repositories to preserve history using grafting

in #git6 years ago (edited)

Why

Grafting is usually required on a software project when migrating from one version control system to other preserving the history of all the commit list previously made prior to migration or when merging the two master branches from 2 repositories of the same project.

Sample Steps to graft using git replace

In this case, I will show you how to pull all the history from a legacy repository (legacy-repo.git) to latest repository (latest-repo.git)

git clone latest-repo.git
cd latest-repo
git remote add legacy legacy-repo.git

Now check if remote repository has been applied
git remote -v

You will see following

legacy [email protected]:<user>/legacy-repo.git (fetch) 
legacy [email protected]:<user>/legacy-repo.git (push) 
origin [email protected]:<user>/latest-repo.git (fetch) 
origin [email protected]:<user>/latest-repo.git (push)

Now fetch files from legacy repository using
git fetch legacy

Now Create local legacy branch from the legacy repository
git checkout -b legacy legacy/master

Now use git log to find the last commit of the legacy-repo
git rev-parse --verify origin

You will see something like below

6ef9179f1d3f1f16a7b5c836365d8b98834f4a7b  

Now tag this with name 'last'
git tag last 6ef9179f1d3f1f16a7b5c836365d8b98834f4a7b

Now checkout latest repo master branch by
git checkout master

Get the first commit of the latest repository (latest-repo.git)
git log --max-parents=0

You should see something like this

commit 4c3e193a473b305857413587845a5c9e722533f4 

Now tag it with name first
git tag first 4c3e193a473b305857413587845a5c9e722533f4

Now verity tag list
git tag

You will see

first
last

Now Graft using git replace
git replace first last

Now remove legacy remote url
git remote rm legacy

Now verify if the legacy url is removed by
git remote -v

You will see only origin with latest-repo.git

origin [email protected]:<user>/latest-repo.git (fetch) 
origin [email protected]:<user>/latest-repo.git (push)

Now push the legacy repository with histories to latest repo
git push --set-upstream origin legacy

Done!

You should see the full history of legacy and current repository on your commit list.

Sort:  

Congratulations @sushrest! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.24
TRX 0.25
JST 0.039
BTC 93333.46
ETH 3336.69
USDT 1.00
SBD 3.26