leonsas leonsas - 11 months ago 39
Git Question

Re-applying changes that were merged and undo'd through Github

I accidentally merged a branch to

through a Github PR. Immediately I reverted it (through github as well). After a few days, when that code was tested and ready to be merged, a merge and PR wouldn't actually get the code into master, because the commit already exists in github (but the code doesn't, since it was reverted.

So now I'm trying to get those changes back to master, after a weeks worth of unrelated commits. This is roughly what
git log --oneline
outputs with some comments on the relevant points:

54c73ee (HEAD, origin/master, origin/HEAD, staging) Merge pull request #637 from leonsas/last-night-view-metric-ceiling
More unrelated changes that should stay in.
af602f0 Merge pull request #639 from leonsas/staging
67ded36 (origin/staging) Merge <-- Here's where I tried merging the changes again, but it doesn't make it into the codebase.
Bunch of unrelated commits that should stay.
a603d0b Merge pull request #633 from leonsas/revert-628-hr-hrv-audit
c3da670 (origin/revert-628-hr-hrv-audit) Revert "Hr hrv audit"
01f2fab Merge pull request #632 from leonsas/revert-629-always-get-hrvz
5824fc8 (origin/revert-629-always-get-hrvz) Revert "Always get hrvz" <-- I reverted changes, because code wasn't tested
b75a537 First iteration at setting the max value on the chart
6939035 Merge pull request #631 from leonsas/is-valid-fix
87b53d5 Merge pull request #629 from leonsas/always-get-hrvz <-- These changes I want in
5b9a848 Merge pull request #628 from leonsas/hr-hrv-audit

What would be the best way of reapplying those changes back into master?

-- Update 1

As per Thomas suggestion, I tried the rebase solution again:

> git checkout -b hrv-almost-latest-changes e1d0d7b
> git rebase master-clone
First, rewinding head to replay your work on top of it...
Fast-forwarded hrv-almost-latest-changes to master-clone.
> git push --set-upstream origin hrv-almost-latest-changes

But then the
is fully up to date with
, so there's nothing to merge on a github PR.

-- Update 2

Generally, cherry picking worked fine. Specific solution was:

git checkout master
git checkout -b hrv-merge-fix
git cherry-pick -m 1 87b53d5
(solve conflicts)
git add <files from solved conflicts>
git cherry-pick --continue
git cherry-pick -m 1 5b9a848
git push origin hrv-merge-fix

Answer Source

I am not too familiar with what github does "behind the scenes", so I would continue this in your local repository on the command line.

You should be able to use git checkout master ; git cherry-pick -m X 87b53d5. Refer to the git help cherry-pick for the value of X:

   -m parent-number, --mainline parent-number
       Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the
       mainline. This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to
       replay the change relative to the specified parent.