Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replay a commit onto a branch that already contains the commit?

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.

like image 723
Kevin Wheeler Avatar asked May 29 '15 23:05

Kevin Wheeler


1 Answers

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

like image 129
Michael Durrant Avatar answered Oct 28 '22 07:10

Michael Durrant