Search code examples
gitcherry-pick

How to do cherry-pick only if no other commits update the files updated by the cherry-picked commit?


For example, consider the following directory:

one
two
three

And the directory has the following commit history and branches:

dev                           master
 |                              |
commit1 --> other commits --> commit-wanted

commit-wanted updates files two and three.

After checking out dev branch, I want the cherry-pick of commit-wanted to succeed only if other commits between commit1 and commit-wanted do not update two and three files.

Is it possible?


Solution

  • Actually you do not need git diff-tree, git rev-list already have what you need.

    git rev-list dev..master^ -- two three
    

    After -- you can specify files changed by target commits. So, this command returns all the commits between dev (excluded) and master (excluded) that modified two OR three.

    Eventually, You can solve that by running a simple command from dev:

    test -z $(git rev-list dev..master^ -- two three) && git cherry-pick master