1

I have an old commit I'd like to cherry-pick onto another branch. However, since it's an old commit there could be mistakes in it that have been fixed by more recent commit. I know it's not flawless, but if I could get a list of the commits that modify the code that was added, that would probably catch any such changes.

I can't go through individual commits since the original because there are too many of them, but the smaller list I described should be manageable.

An example of what I'm looking for:

base:

void foo() {
    //do something
}

after commit1:

void foo() {
    //do something
}

bool bar() {
    return true;
}

after commit2:

bool foo() {
    //do something
    return false;
}

bool bar() {
    return true;
}

after commit3:

bool foo() {
    //do something
    return false;
}

bool bar() {
    if (cond)
         return false;
    return true;
}

if commit1 is the old commit I'm interested in cherry-picking, I should get a list containing commit3, but not commit2.

Edit: To clarify a little further in response to @ObsidianAge's comment, I'll give an example of the cherry-pick.

if the commit history looks like this:

* c470ee1 - commit3 (branch1)
* c65a8d0 - commit2
| * fe51fc2 - commitB (HEAD -> branch2)
| * 900ca02 - commitA
* | 607e2c7 - commit1
|/  
* Base

Then cherry-pick command would be git cherry-pick 607e2c7. However, since commit3 is a fix for a mistake from commit1, the result would be that branch2 now contains the mistake from commit1, but not the fix for it.

Since commit3 modifies the block of 3 lines which commit1 added, this should get it flagged as relevant. On the other hand, commit2 doesn't touch those 3 lines, and should not get flagged as relevant.

I'm aware that it's rather unlikely there is a git porcelain command for this, but I'm hoping there's a way of accomplishing this by combining git plumbing commands and/or terminal commands like sed.

Daniel McIntosh
  • 522
  • 3
  • 17
  • Why would you get `commit3` but not `commit2`? `commit2` has changed -- it added a `return false`. A simple cherry-pick of the original commit will give you the *entire* changes between that commit and the current state though -- if you want to find out which *commits* those changes came from then you may need to do it manually. – Obsidian Age Sep 09 '19 at 23:14
  • you can alsways get a list of commits which modified your particular file. this should give you a smaller lilst of commits to check. – Serge Sep 10 '19 at 00:38
  • @ObsidianAge I've added further clarification to the question. Commit2 is irrelevant to the changes from commit1, the changes don't occur on any of the lines added by commit1. Put another way, commit2 could be cherry-picked onto branch2 without commit1, but trying to cherry-pick commit3 onto branch2 without commit1 would result in a merge conflict. – Daniel McIntosh Sep 10 '19 at 20:35

1 Answers1

1

As seen in "how do I view the change history of a method/function?", you could use a git log -L :<funcname>:<file>

That would help trace the commits which have modified a particular function.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This only works for the small example I gave. In reality commit1 is spread across multiple methods in multiple files, and much too large for this to be practical. Furthermore, some of the changes commit1 makes could be only 2-3 lines of a 50+ line function. – Daniel McIntosh Sep 10 '19 at 20:39
  • @DanielMcIntosh I agree, that would not be ideal in your case. – VonC Sep 10 '19 at 20:44