Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace remote repository with local repo, but keep commit history of remote

Tags:

git

github

I have an Android "stored" in a Github repository. Now I did a complete rewrite of the app (started from fresh in another repository) - (how) is it possible to push the complete new repository to the existing one, but keeping it's commit history?

I guess I do not want to use the -f flag, as the commits are removed then. The best case would be to have the complete history of both repositories, but the code should only be from the new one.

like image 660
swalkner Avatar asked Oct 02 '15 15:10

swalkner


1 Answers

Here's a brute-force approach (assuming you are working on the master branch):

Go to the old, original repo, rm -Rf everything but the .git directory. Then:

git add -u
git commit -m "starting over"

Then, define a remote for the old repo in your current repo:

git remote add origRepo oldUrl
git fetch origRepo

Then, create a temporary branch in your current repo and push your code:

git checkout -b tempBranch
git reset --hard origRepo/master
git merge master
git push origRepo tempBranch

Then on the original repo:

git fetch
git checkout master
git merge origin/tempBranch
git push origin master

You could also look into using an ours/theirs merge strategy if not every single file needs to be replaced.

like image 192
Jonathan.Brink Avatar answered Sep 28 '22 11:09

Jonathan.Brink