How to move commits of a branch in a remote git repository, to a new remote repository keeping commits history in both repositories

Image courtesy: https://kanbanize.com/blog/kanbanize-github-integration/

Recently I wanted to move all the content from my github repository to a repository which was managed by the company I am working now. You may say that It is really easy because I can fork that repository to my github account and do the changes and send a Pull Request to the main repository. But in my case I created my own repository long time ago, the company created its repository. So in this case company repository has a newer commit history than my commit history. (my commit history starts long time before the company one starts) The requirement is, to push all my commits into the company repository. This is how I did it.

Let’s say SampleA is my repository and SampleB is the company owned repository.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —SampleA has following files:

A.txt

Common File.txt

README.md

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —SampleB has following files:

B.txt

Common File.txt

README.md

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — The final result should be:

A.txt

B.txt

Common File.txt

README.md

files stored in SampleB.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

  1. First clone the SampleA into you computer.
git clone https://github.com/GithubUserName/SampleA

2. Then fork the SampleB into your github account and clone it to your computer.

git clone https://github.com/GithubUserName/SampleB

3. Then go into the SampleA.

cd SampleA

4. Checkout to the branch you want to push commits to SampleB. In my case it’s master branch.

git checkout master

5. Remove the remote origin.(Before doing this you can check the remotes by using the following command)

git remote -v

Perform:

git remote rm origin

6. Add the new remote which is SampleB.

git remote add origin https://github.com/GithubUserName/SampleB

7. Then fetch from the new origin.

git fetch origin

8. After that perform a git rebase.

git rebase origin/master

9. Performing the git rebase will result in merge conflicts. These merge conflicts are raised because of the commits done to the common files. In this case it is because of Common File.txt and README.md have different content in SampleA and SampleB. To overcome this problem you have to resolve all the merge conflicts raised. You can find how to resolve merge conflicts in here. Each time after you resolved a merge conflict, add/remove that file and perform:

git rebase --continue 

or

git rebase --skip

10. After performing a successful git rebase now you have all the commits in SampleA and SampleB in the SampleB local repository. Push all the commits to SampleB repository in your github account, by performing the following command.

git push -f origin master

11. Now you can send a Pull Request to the original SampleB repository(in my case, the company owned repository) with all your commits.

You have successfully moved commits of a branch in a git repository, to a new remote repository keeping commits history in both repositories. :)