0

I have a repo with a single branch and 10 commits. I want to create a patch for all the commits affecting two of the files in the repo, say for example commits #3, 4, and 7, ie not the head, not in sequence, and not labeled. I've kept the scope of commits small so they don't affect unrelated files.

How should I do this?

I've looked at lots of examples online including cherry-pick, squash, and checking out a specific commit in a temp branch, but none of these appear to be close to my scenario, and my head is spinning at all the possibilities.

JohnC
  • 1,797
  • 1
  • 18
  • 26

1 Answers1

1

Make a temporary branch

git checkout -b tmpb  #3^

Cherry-pick commits #3, 4 and 7 into the branch

git cherry-pick #3
git cherry-pick #4
git cherry-pick #7

Squash them into a single commit

git rebase -i #3^

Use git format-patch to create a patch

git format-patch master

Note, that #3, #4 and #7 in commands should be replaced with respective SHA1

Konstantin
  • 24,271
  • 5
  • 48
  • 65
  • What is the significance of the carrot "^"? Why do I need to cherry pick #3 when I create the branch based on that commit? Thanks very much for that procedure. – JohnC Apr 28 '15 at 10:40
  • `#3^` means parent of commit `#3`. Read more about specifying revisions [here](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html) – Konstantin Apr 28 '15 at 10:44
  • 1
    Do `git cherry-pick --no-commit #3 #4 #7; git commit -m-` to avoid the rebase. If you really just want the diffs you can skip the branch creation and the commit, and just `git diff --cached` – jthill Apr 28 '15 at 11:06
  • Thanks very much for the answer, worked great. For the patch I used `git format-patch master --stdout > patch-name.patch`. I like the idea of avoiding the rebase so I'll try @jthill variant next time. – JohnC Apr 29 '15 at 01:59