How do I replay a commit on a branch that already contains a commit?

Let's say our commit history looks like

    1-2-3-4
             ^
            HEAD

Where 4 is the last commit.

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

You might be wondering why someone would want to do this. Let's say this is your manufacturing industry, which should always be in working order. Let's say that earlier when the commit history looked like

    1-2
       ^
      HEAD

You had an implementation and thought that commit 2 could break everything, and so you quickly pop a revert commit when commit 3 reverses commit 2. Then someone does commit 4 which contains some nice content you want to keep. At this point, you realize that commit 2 is actually ok and therefore you want to replay it on top of 4.

+3


source to share


3 answers


git cherry-pick A

can do it.

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



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

+6


source


You might be wondering why someone would want to do this. Let's say this is your manufacturing industry, which should always be in working order. Let's say that earlier when the commit history looked like

1--2
   ^
  HEAD

      

You had an implementation and thought that commit 2 could break everything, and so you quickly pop a revert commit when commit 3 reverses commit 2. Then someone does commit 4 which contains some nice content you want to keep. At this point, you realize that commit 2 is actually ok and therefore you want to replay it on top of 4.

Then you want to revert commit 3 (which returns commit 2), so restoring commit 2:



git revert 3

      

+1


source


What do you need is to revert the changes made to 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.

0


source







All Articles