Cherry-Pick/Merge changes from one repo to another repo
I was working on a random feature of next release on an usual day at my org, since I had already completed all my tasks of the current release. Around 4PM that day, my UI Architect called me for a help regarding cherry-picking few commits from a client repo to our core repo, which sounded little strange initially, but there is a whole another story to that.
I have done cherry-pick a numerous time in a same repo, but this was a first time where I had to this. After struggling for few minutes I found the way. It sounded little difficult at first, but after understanding it really became simple and even made me to write this blog post.
Assume there are 2 Repos:-
- Repo A (github.com/abcd/repo-A)
- Repo B (github.com/abcd/repo-B)
And we need to commit few changes from repo A to repo B
First we need add a new origin of repo A to the target repo B.
$ cd /path/to/repo-B
$ git remote add repoA https://github.com/abcd/repo-A
After that we need fetch all the changes of that origin
$ git fetch --all
Now go to Repo A and find the commits which we want to add to repo B.
git cherry-pick
is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD. Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes.
$ git cherry-pick commitSha1 commitSha2 commitSha3 commitSha4
If we are faced with any conflicts in this stage, we can resolve them in our text editor and then continue the process by :-
$ git cherry-pick continue
Similarly we can also merge their changes from the repo A if we want all the changes
git merge
is used to combine two branches.It takes two commit pointers, usually the branch tips, and will find a common base commit between them. Once Git finds a common base commit it will create a new “merge commit” that combines the changes of each queued merge commit sequence.
$ git merge repoA/master
and if faced with any conflict, we can continue this process after resolving them.
$ git merge --continue