14

Lets say our commit history looks like

    1--2--3--4
             ^
            HEAD

Where 4 is the most recent commit.

Is there a way to replay the changes from commit 2 (the differences between 2 and 1) onto commit 4?

You are probably wondering why anyone would want to do that. Lets say that this is your production branch that is always supposed to be in a working state. Lets say that earlier, when the commit history looked like

    1--2
       ^
      HEAD

You had a realization and thought that commit 2 might break everything, and so you quickly pushed out a revert commit, where commit 3 reverts commit 2. Then someone makes commit 4 that contains good content you want to keep. At this point you realize that commit 2 was actually ok, and so you want to replay it on top of 4.

1
  • Which direction does your history go? Is commit 4 the parent of commit 5 or vice versa? May 30, 2015 at 0:02

3 Answers 3

17

git cherry-pick A can do that.

See http://git-scm.com/docs/git-cherry-pick
Given one or more existing commits, apply the change each one introduces, recording a new commit for each. This requires your working tree to be clean (no modifications from the HEAD commit).

For example git cherry-pick master~4 master~3

2
  • git cherry-pick A creates an empty commit since the commit is already present. Specifying --keep-redundant-commits would just create an empty commit and keep it. Any way to actually apply the difference between 1-2 to the HEAD?
    – Pratik
    Jun 23, 2020 at 23:19
  • You would need to drop it first with git rebase -i sha-commit-1 and then cherry-pick with git cherry-pick sha-commit-2 to create a new commit with the changes of the dropped commit on top.
    – Anateus
    Jan 17, 2022 at 12:00
4

You are probably wondering why anyone would want to do that. Lets say that this is your production branch that is always supposed to be in a working state. Lets say that earlier, when the commit history looked like

1--2
   ^
  HEAD

You had a realization and thought that commit 2 might break everything, and so you quickly pushed out a revert commit, where commit 3 reverts commit 2. Then someone makes commit 4 that contains good content you want to keep. At this point you realize that commit 2 was actually ok, and so you want to replay it on top of 4.

Then you want to revert commit 3 (which reverts commit 2), therefore restoring commit 2:

git revert 3
1

What do you want is revert the changes introduced in commits 3 and 4? If so, try:

$ git diff commit2..commit4 | git apply -R

This only works if the history is linear between the commits you want to revert.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.