swalkner
swalkner

Reputation: 17379

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

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.

Upvotes: 9

Views: 2725

Answers (3)

raduken
raduken

Reputation: 2129

Can be more simple:

List your existing remotes in order to get the name of the remote you want to change.

$ git remote -v

origin  [email protected]:USERNAME/REPOSITORY.git (fetch)
origin  [email protected]:USERNAME/REPOSITORY.git (push)

Change your remote's URL from SSH to HTTPS with the git remote set-url command.

$ git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git

Verify that the remote URL has changed.

$ git remote -v
# Verify new remote URL
origin  https://github.com/USERNAME/OTHERREPOSITORY.git (fetch)
origin  https://github.com/USERNAME/OTHERREPOSITORY.git (push)

The next time you git fetch, git pull, or git push to the remote repository, you'll be asked for your GitHub username and password.

more info at:

https://help.github.com/articles/changing-a-remote-s-url/

Upvotes: 0

You can commit into the same repository completely separate commit graphs. You remote repository will keep both trees as long as there are branches pointing to each. This approach may be a bit confusing for users of the repo as it is quite uncommon to have more that one commit-tree per repository, but its an entirely valid use of git.

Assuming you want master in your remote to point to the new repository:

# Rename and push your old master
cd oldRepo
git branch old-master master
git push remote old-master

# Push your new master
cd newRepo
git remote add remote url-to-remote
git push --force remote master

After this the master of your remote will point to the new commit tree, and the old tree will still be there accessible through old-master. If you checkout old-master your whole directory structure will be overwritten with the old contents, and likewise if you checkout master.

These two trees could also be merged into a single graph. It would look like a merge where the entire repository was rewritten, because that is exactly what it is. If you do this then both histories are retained because they are still accessible in the commit-tree:

# Fetch your remote in your new repo
cd newRepo
git remote add old-remote url-to-remote
git fetch old-remote master
# Assuming current branch in newRepo is master
git merge old-remote/master
git push old-remote

Upvotes: 0

Jonathan.Brink
Jonathan.Brink

Reputation: 25443

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.

Upvotes: 5

Related Questions