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

Fri Oct 02 2015

Paul Shan

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.

//adding the 2nd repo as a new remote
$ git add remote source [email protected]:myusername/anotherrepo.git 

//fetch the branch which contains your required commits
$ git fetch source/requiredbranch 

//just to log the commit list 
$ git log source/requiredbranch 

//Press q to come out of the logs

$ git cherry-pick --signoff requiredCommitNumberOrSHA

$ git push //pushing the cherry pick

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.

$ git --git-dir=../thirdPartyRepoPath/.git format-patch -n commitIdOrSHA
//n : number of successive commit you wanna take
// commitIdOrSHA : starting from this specific commit.

// the name of the patch will be printed 
0001-some-commit-msg.patch

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).

$ git am 0001-some-commit-msg.patch

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.

$ git add remote source [email protected]:myusername/anotherrepo.git //adding the 2nd repo as a new remote

$ git fetch source/requiredbranch //fetch the branch which contains your required commits

git checkout -b myPatchBranch source/requiredbranch

// the code above will take you to the new branch myPatchBranch 
//which will be a clone or source/requiredbranch and nothing else.

$ git format-patch -n commitIdOrSHA
//n : number of successive commit you wanna take
// commitIdOrSHA : starting from this specific commit.

// the name of the patch will be printed 
0001-some-commit-msg.patch

//go to your actual branch
$ git checkout myActualBranch

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.

git am --signoff 0001-some-commit-msg.patch

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.

SHARE THIS ARTICLE

post-thumbnail
Today everyone knows the importance of a lightning-fast website and how the speed impacts the conversion rate of a business. Today, everyone wants the site to be a PWA so that the mobile users can have an app-like experience with the website because, for the majority of the merchants, the customers come through mobile devices.
Tue Apr 20 2021
post-thumbnail
Here we are going to see how you can manage backup and restore of Postgres database with docker.
Thu Sep 03 2020
post-thumbnail
Image sliders or carousels always have increased the UI attraction of websites and they are pretty useful for reflecting the major roles/products too. In case, I am having a website that sells tee-shirts,
Mon Apr 30 2018

About VoidCanvas

This blog was created out of hobby and talks mostly about technology, web development, JavaScript, NodeJS and related topics. Thank you for reading my blog.

Copyright 2022 - www.voidcanvas.com

Popular Articles

Authentication using Google's oAuth api with node.js

Thu Mar 10 2016

OAuth authentications are pretty popular now a days and another thing which is popular is JavaScript. This article shows how to plugin google’s oAuth api for authentication in your own node application.

CSS3 Loader Snippet Collection: (Part 2 - Squares)

Sat Mar 01 2014

This is a continuation of my CSS3 loader snippet collection series. I've provided spinning css3 animation loader in the part 1 of this series and here in part 2, I'm providing various square type loading