githow-totutorial

How to: Move commits between repositories

Oct 19, 2022

This is my first how-to ever. I'll keep it short and direct, because I hate those tutorials that keep going around the bush.

What are you trying to solve?

You want to transfer code from one repository to another, but you also want to keep that code's change history in the new repo.

How to do it?

First, let's name things properly: source repo is the repository where the code is coming from, destination repo is the one that's receiving the code.

  1. Add the source repo as a remote to the destination repo:
# you should be inside the destination repo, in the desired branch
$ git remote add source-repo <REPOSITORY LOCATION HERE> # it can be either a url from Github/etc. or the path to a repository in your computer
  1. Fetch the commits from remote:
$ git fetch source-repo # you can specify a branch if you want
  1. Find the commits you want to move. You can use any GUI client or the command line:
$ git log source-repo/<BRANCH>
  1. Move the commits. Beware of merge conflicts!
$ git cherry-pick <COMMIT HASH> # check the docs to find more ways to use this command
  1. (Optional) Remove the source-repo from the destination repo:
$ git remote remove source-repo

Wrap up

As you can see, it's pretty simple, although this trick uses some less known Git commants. Any questions or suggestions, send me a message!