0

How to apply the same changes (in several commits) to different branches?

First of all, this is different from How to copy commits from one branch to another? as the purpose is different, thus the answer there was specific for that question.

I've got a project that has a master branch and a stable branch - the branches diverged long time ago. Now I need to backport a fix to that stable release branch -- I've got a couple commits on the master branch, which I also want to have on the stable branch (a bug fix). I cannot merge, as the branches diverged and there's loads of unmerged changes - I just want my 8 commits.

Actually the answer from How to cherry pick a range of commits and merge into another branch? is more close to what I'm looking for,

However that answer begins with:

When it comes to a range of commits, cherry-picking was not practical.

and didn't give clear git command for people to follow.

UPDATE: The proposed answer GIT cherry pick all my commits that exists in one branch to another branch does not solve my problem as I've explained in the comment right away -- that question, "I want to cherry pick all commits that doesn't already exist in branch A from branch B to branch A without the need to manually search for every single one", is different than what I'm asking here.

xpt
  • 20,363
  • 37
  • 127
  • 216
  • With only 8, just cherry pick. – jonrsharpe Feb 19 '19 at 21:20
  • @jonrsharpe, yes. So what's clear git command that cherry pick the range of my commits, that I've moved to the top? – xpt Feb 19 '19 at 21:22
  • https://git-scm.com/doc – jonrsharpe Feb 19 '19 at 21:23
  • I believe almost every single question asked on SO have answers _"encrypted"_ in some man pages somewhere. So do all the 107,843 `git` related questions in SO. – xpt Feb 19 '19 at 21:31
  • You may give my answer here a try: https://stackoverflow.com/questions/51437676/git-cherry-pick-all-my-commits-that-exists-in-one-branch-to-another-branch/51439316#51439316 – Timothy Truckle Feb 19 '19 at 21:40
  • 1
    Possible duplicate of [GIT cherry pick all my commits that exists in one branch to another branch](https://stackoverflow.com/questions/51437676/git-cherry-pick-all-my-commits-that-exists-in-one-branch-to-another-branch) – Timothy Truckle Feb 19 '19 at 21:40
  • @TimothyTruckle, thanks, but that question, _"I want to cherry pick all commits that doesn't already exist in branch A from branch B to branch A without the need to manually search for every single one. "_, is different than what I'm asking here. – xpt Feb 19 '19 at 21:44

1 Answers1

2

When it comes to a range of commits, cherry-picking was not practical.

The "problem", if you can call it so, is that if you wish to pick several commits and they are in sequence its an excellent solution.

But if you wish to exclude several commits you will need to cherry-pick them in segments.

For example: Given the following commits: 1-2-3-4-5-6-7-8-9 if you wish to cherry-pick 1-3 and 5-7 you have to break it into segments:

git cherry-pick 1.. 3 5..7

So what's clear git command that cherry pick the range of my commits?

The above command.

# (2 dotes) Pick all commits in the range from a to be 
git cherry-pick a..b

# (3 dotes) pick all commits which are not in both branches
git cherry-pick a...b

# pick a single commit
git cherry-pick a

# pick commit range(s)
git cherry-pick 1..3 5 6 7
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Thanks. So if I've moved all my commits to the top, i.e., they are in sequence, then all that I need to do is, `git cherry-pick SHA-COMMIT-MY-FIRST..SHA-COMMIT-MY-LAST`, right? – xpt Feb 19 '19 at 21:48
  • its a typing error, i updated the answer, thank you for your notice – CodeWizard Feb 19 '19 at 21:52
  • thanks for fixing them. Can you confirm my above _"if I've moved all my commits to the top"_ question please? – xpt Feb 19 '19 at 21:55
  • well, [found](https://stackoverflow.com/a/3664543/2125837) that _"`cherry-pick A..B` will not get commit A (you would `need A~1..B` for that)"_ – xpt Feb 19 '19 at 22:16