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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //adding the 2nd repo as a new remote $ git add remote source git @ github . com : 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.
1 2 3 4 5 6 | $ 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).
1 | $ 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ git add remote source git @ github . com : 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
1 | git am -- signoff 0001 - some - commit - msg . p |
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.