1

Cliffnotes version

  • Assume I want to run this command: git cherry-pick ABC..XYZ. I want ABC to be included in my commits to the current branch. Do I have to reference the commit before ABC?

Long Version

  • So I have branches master, Apple and NewApple.
  • Apple was built upon a very old version of master but only worked on a dozen files which were implemented on folders which the rest of the master did not really touch.
  • Apple has commits ABC, BCD, and XYZ. These are commits unique to Apple. Please note there are about 10 commits between BCD and XYZ. These commits were NEVER commited tomaster`
  • So I made a new branch NewApple which is spun off of a newer versions of master, ones with newer commits.
  • Then I cherry-picked commits from Apple to put onto NewApple like so git cherry-pick ABC..XYZ
  • However, when finished and committed to my remote repository, I noticed that ABC was not included. Everything after it and including XYZ were put onto NewApple.
  • I want to be able to commit ABC as part of my cherry-pick without referencing the commit that is before it in Apple. I am afraid that if I do, I won't get all the commits in Apple or it will try to merge in random older master commits on top of newer versions of master.
munchschair
  • 1,593
  • 3
  • 19
  • 43

1 Answers1

6

... git cherry-pick ABC..XYZ [but] I want ABC to be included in my commits to the current branch. Do I have to reference the commit before ABC?

Short answer: yes.

Longer variety of answer: Git has a syntax for that, since it's such a common requirement. Any name suitable for identifying one specific commit, such as master or a123456, can be suffixed with ^ or ~ characters followed by numbers. The default number is just 1, and hence:

master^

or

master~

means "the commit before the one selected by the name master".

In this case, then, you might write:

git cherry-pick ABC^..XYZ

Note that Windows-y command line interpreters tend to treat ^ as an escape character, requiring entering the command using ABC^^..XYZ or "ABC^..XYZ", so there you might prefer ~. Either one works here.

(More specifically, ^number means the number-th parent, while ~number tells Git to count back that many first-parents. So master~3 means the same as master^1^1^1. Since 1 is the default number, you can write this as master^^^ as well. There are many more ways to identify commits, all outlined in the gitrevisions documentation.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • This is exactly the kind of thing I was looking for. I learned a lot! – munchschair Dec 29 '17 at 16:25
  • @Andry: to me, that seems weird, because I don't use Windows. :-) Also all the programming languages I use most use backslash as the escape (C, C++, Go, Python, etc). But I'll edit. – torek Dec 30 '19 at 13:46