How to take commits from a different repository : Git cherry-pick & format-patch

Don’t know how many of you faced this situation to cherrypick or pull latest commits from an entirely different repo. But this situation is very obvious if you are customizing an existing product for a client and the base product is also getting updated frequently by the team of that.

This article talks about

The articles talks about different processes to to pickup your changes from a different repository. Processes are mentioned in the bullet points below.

By cherry-pick

Cherry picking is a nice process to pickup selective commits from somewhere. It’s easy when you are doing inter branch of the same repo; but a little tricky if you wish to get the commits of a different repository. Below is the code to do so.

What we did here is pulled a certain branch of the repo you want to get commits from. Fetching doesn’t merge the changes of that repo to your existing one (if you want all changes to be merged to your current repo branch you need to use $ git pull)

You can also use git gui to cherry-pick the commits after fetching. Git gui is a tool which is auto installed in your device with git.

–signoff helps to keep track of the person who cherry-picks.

By creating a commit patch

If you have a hell lot of commits to get, the previous way of cherrypicking selective ones may become a stiff task. In that case you can create a patch of multiple successive commits and apply it to your current one. This can be done in two ways. One, cloning the third party repo in some other directory. Two, fetching the required branch of the third party repo in some other branch of your repo.

Way 1 to create patch

Assuming you have the third party repo cloned in your machine and the current branch there is the one from where you want to pick the commits.

So your patch file is created and available with your current repo. You can check the status of the patch using the following command (The command will not apply the patch changes to your directory).

Way 2 to create patch

If you don’t have or don’t want to clone the repo in a separate folder, you can create a branch in your own repo which will hold the changes of the third party repo. After that create the parch from there. Below is the code.

Apply the patch commits

Well, now your patch file is ready to be applied (using any of the two ways mentioned above). Let’s apply it with the commands below.

This will apply the changes to your repo. If conflicts appears you need to resolve them and continue.

–signoff helps to keep track of the person who applied the patch.

About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.

  • justrajdeep

    u made my day 🙂

  • Floris

    There are a number of errors in the section ‘Way 2 to create patch’, at least with git 2.5.4 I have installed here:

    – instead of ‘git add remote’ — ‘git remote add’
    – instead of ‘git fetch source/requiredbranch’ — ‘git fetch source requiredbranch’

  • susheel

    Its really Awesome information

  • MichaelFreidgeim

    Could you please update the typos, that Floris listed below?