Home > Mobile >  How can I push an unrelated history as a series of new commits?
How can I push an unrelated history as a series of new commits?

Time:12-18

I have two separate remote respositories, RemoteOld and RemoteNew.

RemoteOld
Uri: http://RemoteOld
Branches: A--B--C--D--E master

RemoteNew
Uri: http://RemoteNew
Branches: A'--B' master

I want to take all of the commits from RemoteOld and add them to RemoteNew, so that RemoteNew looks like this:

RemoteNew
Branches: A'--B'--A--B--C--D--E master

Commit A in RemoteNew/master should effectively remove all the files in RemoteNew and replace them with the files in a snapshot of RemoteOld/master at Commit A. Commits B-E should be the same between RemoteOld and RemoteNew. I need to do this without using a force push.

CodePudding user response:

You have to rebase RemoteOld/master onto RemoteNew/master. Since both branches have the same name you will need to temporarily rename a branch:

  1. Clone RemoteOld:

     git clone http://RemoteOld
     cd FolderCreatedByClone
    
  2. Add RemoteNew. You can remove RemoteOld while you are at it because this won't be used anymore:

    git remote add RemoteNew http://RemoteNew
    git remote rm origin
    
  3. Rename RemoteOld/master to some other name, like masterOld. Fetch the master branch from new origin. Then run git branch -a to see all the branches:

    git branch -m masterOld
    git fetch RemoteNew
    git branch -a
    

Output:

 *masterOld
 remotes/RemoteNew/master       
  1. Rebase masterOld onto RemoteNew/master:

     git rebase --onto master masterOld
    
  2. At this point masterOld will look like what you want RemoteNew/master to look like, so just merge masterOld into RemoteNew/master.

    git checkout master
    git merge masterOld
    
  3. Push RemoteNew/master. The old master branch can be deleted.

    git branch -d masterOld
    git push
    
  • Related