Git cherry-pick diff between commits without a common history?

Scenario: a repository with completely unrelated history. For instance. from different remotes that do not have a common git history. (This usually happens in git subtree scripts.)

Is it possible to use cherry-difference between the two commits, even if they have no common history? (And active commit has no common history with either of these two)

I tried this (in a subdirectory with the -X subtree=sub/dir option):

 git cherry-pick -X subtree=vendor/package aaa/aaa..bbb/bbb 

Without a subtree, this would be simpler:

 git cherry-pick aaa/aaa..bbb/bbb 

Where aaa / aaa and bbb / bbb are two commits with disabled history.

Unfortunately, this does not work: aaa / aaa..bbb / bbb does not read as diff, but something else.

You can show the same thing with git show aaa/aaa..bbb/bbb , which is very different from git diff aaa/aaa bbb/bbb . For instance. if both have the same files, then git diff will be empty, but git show a..b will only show b, but not.


Note. My personal use case is with a subtree background. I mention this to avoid using artificial use, when people usually start discussing the validity of a use case instead of the actual question.

The ideal answer will first consider the general case, and then turn to the subtree case.

+5
source share
2 answers

Since the pick cherry uses diff, it would be easier to create a patch and apply it :

 git checkout aaa/aaa git cherry-pick -n bbb/bbb git diff --cached > my.patch 

Then check the correct branch and git apply my.patch

This takes commit ' bbb/bbb ', compares it to its immediate ancestor, then applies this difference on top of aaa/aaa : conflicts can be expected.
Check if -X subtree=vendor/package supports in case of subtree.

+2
source

@VonC points in a good direction. Credit and +1 for this idea.

it would be easier to create a patch and apply it:

However, this can be done with a simple one-liner (I tried this and it works for me):

 git diff aaa/aaa bbb/bbb | git apply --directory=vendor/package 

Or in a script without a subtree:

 git diff aaa/aaa bbb/bbb | git apply 

No need for git checkout, git cherry pick or local file creation.
Then you still need to fix this material, obviously.

And here's how to do it without adding and / or not getting remote pools. Instead, you can clone package repositories into separate directories outside the main repo of the project.

 git --git-dir="/path/to/package/repo/.git" diff aaa bbb | git apply --directory=vendor/package 

As I understand it, all this is basically equivalent to a regular git subtree . I mean, the history of the main project will be git. Correct me if I am wrong.

+2
source

Source: https://habr.com/ru/post/1241969/


All Articles