5

I am trying to create a git patch from multiple commits in my branch. However, I need to create it from arbitrary commits (they wont necessarily be in a range). In between the commits I want to create a patch out of, some of the commits change some of the files in the patch may have also been changed.

Here is my use case (assume greater number = later commit date):

develop HEAD
Commit 5 - Changed a.txt
Commit 4 - Changed a.txt, b.txt, c.txt
Commit 3 - Changed b.txt, c.txt
Commit 2 - Changed a.txt
Commit 1 - Changed a.txt, b.txt, c.txt, d.txt
master HEAD

Now I want to be able to create a patch for commits 1, 3 and 5 from the develop branch and apply them to the master branch.

I've looked around and all I can find is the ability to create a patch in a range. Is there a way to create a patch for multiple commits with potential changes in the files between them?

Andrew
  • 1,355
  • 2
  • 13
  • 28

1 Answers1

7

Create a new branch from HEAD in master

git checkout master
git checkout -b newBranch

Cherry Pick the commits here.

git cherry-pick commit1SHA
git cherry-pick commit3SHA
git cherry-pick commit5SHA

Create a patch from this.

git format-patch master --stdout > nameOfPatch.patch

Now, you want to move commits 1,3,5 from develop to master. You can simply do this by following the above statements till the last cherry-pick and then :

git rebase -i HEAD~3

Now, pick the first commit and squash the other two. You now have one solitary commit that encapsulates all three of the other commits. Now, just cherry-pick this commmit, say squashedCommitSHA to master.

git checkout master
git cherry-pick squashedCommitSHA
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • The last line writes output in mbox format to a file named `.patch`, which is confusing. – Sven Marnach Jul 30 '15 at 20:16
  • Moreover, my understanding of the OP's problem is that they might want to cherry-pick into master directly, and only asked about creating a "patch" due to the assumption that this might be necessary to get selected commits into master. – Sven Marnach Jul 30 '15 at 20:18
  • Oh I just got that. Thanks! – gran_profaci Jul 30 '15 at 20:54
  • Updated with a way to just move the stuff around to master. – gran_profaci Jul 30 '15 at 20:56
  • This is really easy to do with `gitk`. Just check out the branch you want to modify, then right click -> cherry pick on each commit you want. (In the order you want them, of course.) – Peter Cordes Jul 31 '15 at 05:07