Reputation: 2947
We have the following case:
What is the easiest way to get git Histroy from G1 to G2 without losting the new changes on G2?
Upvotes: 1
Views: 70
Reputation: 11698
I assume you just copied the contents of G1 and created a new repo from scratch, otherwise you would have a history.
If so rebasing G2 on top of the old G1 would do it.
I tested the provided scenario with one of my projects and it results in the desired effect.
1. In G2, add the G1 as a remote
git remote add G1 <Path>
2. Pull G1
git pull G1 main
3. Rebase G2 on top of G1
git rebase G1 main
At this point, if you may get a message:
Current branch main is up to date.
If so, it is safe to rebase, so:
git rebase -f G1 main
Done
Notice that G2 will lose its initial commit, as now the initial commit will be the one from G1. No problem because the contents should be the same.
Inspect/Test the project, make sure everything is ok.
If it is all good, force push G2, as the rebased commits will have their hash updated.
git push -f
Be careful if working on a team, just make sure no one has pending changes to push, ask people to drop then clone the repo again after this operation. As this is a one-time thing it should be ok.
Remove G1 remote
git remote remove G1
Tests
In my scenario, this was the result:
* 0da12b8 (HEAD -> main) G2 Second Commit <--initial commit was lost
* f2dbe6e G1 Second Commit
* d93f003 G1 Initial commit
History starts from G1 initial commit. Commits from G2 are applied on top of the last commit on G1, so their ancestors are reset at that point.
Initial commit on G2 is lost, but it is ok because its contents should be the same as the last commit on G1.
This approach assumes a clean copy from G1.
Tip
To avoid this situation one may copy a project with history to a new repository like so:
git clone <A repo URL> project
cd project
git remote remove origin
git remote add origin <New repo URL>
git push -u origin main
Upvotes: 1