Move all commits to another repository

Satoru Sasozaki
3 min readSep 24, 2016

This is a post written on 2016–1–8 and transferred from sasozaki.com.

Why did I come to do that?

I’m using the Jekyll theme Arctic Fox made by Camille Diez. If you want to use something on someone’s git repository, then you should fork the repository to your account to use it in order to respect the developer and show the credit. However, I didn’t fork the repository. I just created my own repository and took the files from the developer’s repository. Three months have passed and I finally realized this is not good and I have to fork her repository to use it. Since, I have already committed some on my own repository, I had to move all the commits to the new forked repository.

Steps

  1. Fork the developer’s repository and clone it to your local machine
  2. [git rebase -i] Squash all the commits that you have made so far in your old repository into one commit so that it will be moved more easily.
  3. [git cherry-pick] Take the squashed commit from the old repository and merge it to the new forked repository
  4. Fix conflicts if needed

Fork the developer’s repository and clone it to your local

Fork the repository to make it on your account on Github. Make sure to copy the URL of the forked repository.

Go back to your computer.

$ git clone URL

Squash all the commit

Squash all the commits into one commit so that it will be moved more easily.

There were 50 commits in my old repository. You should squash all the commits into one commit if there are many, because it is a pain to move each commit.

First, you need to know how many commits to squash. Go to the repository on Github and remember how many commits in total.

Use git rebase -i

git rebase -i (interactive rebase) is a git command to modify the commit history. I wrote more about git rebase -i in this post. Let’s say the number of commits to squash is 10.

$ git rebase -i HEAD~10

Then, you will see the recipe for the 10 commits.

pick f2ed00w  Change the font
pick ui322bn Modify something
pick HASH COMMIT_MESSAGE
.
.
.

You will squash all the commits except the latest one because you will squash them into the latest commit, so change pick to s (squash) except the latest commit.

pick f2ed00w Change the font
s ui322bn Modify something
s HASH COMMIT_MESSAGE
.
.
.

After that, you will see the commit editing screen. You can modify the message like “Squash all the commits to one commit” and exit the editor.

Push the change to your remote repository. Since you change the commit history, you can’t just push in the usual way like

$ git push origin master

You need to put — force.

$ git push --force origin master

That’s all for squashing.

Take and merge the squashed commit

Take the squashed commit from the old repository and merge it to the new forked repository.

It’s time to merge the commit to the new repository. We will use git cherry-pick , which is a git command to get commits from another branch or repository. In order to get commits from the other repository,

You’ll need to add the other repository as a remote, then fetch its changes. From there you see the commit and you can cherry-pick it.

By CharlesB in the post, Is it possible to cherry-pick a commit from another git repository?

Go to your remote repository where the squashed commit is and get the full hash of the commit. Finally, git cherry-pick. Go to the forked local repository and do following.

$ git remote add oldRepo https://github.com/yourname/your_old_repository_name
$ git fetch oldRepo
$ git cherry-pick 9ac1cb8b8f44d957035f1c4cb0673a927b95cd3d
# put the hash you have got

The squashed commit from your old repository got merged to the new forked repository.

Fix conflicts if needed

There might be some conflicts around some files. Fix them manually and we are done.

--

--