0

Let say I have two git branches:

  1. branch a with files 1 and 2
  2. branch b with file 2

I have a commit in branch a which modified file 1 and 2. I checkout branch b trying to cherrypick this commit but I only want the part of the commit that changes file 2 (because branch b does not have file 1).

How is this possible?

zrrbite
  • 1,180
  • 10
  • 22
motam79
  • 3,542
  • 5
  • 34
  • 60
  • Branches don't "have" files. Branches, to the extent that they are even a thing, have commits. Every commit has _all_ the files. You may have _changed_ just a certain file at the time you made a certain commit, but the commit has _all the files_. It is a snapshot of the _entire_ state of things at the time it is created. – matt Sep 24 '20 at 19:54
  • @matt - OR, the commits on one branch have a file, and the commits on another don't (becuase the file wasn't created in that other branch's history, or was deleted). A commit does not automatically "have all the files". (And to be clear - Yes, in git terms, saying "commit on a branch" is tenuous, and "file on a branch" moreso, but OP's meaning is perfectly clear nonetheless.) – Mark Adelsberger Sep 24 '20 at 20:13

1 Answers1

0

You can just use cherry-pick normally. The attempt to apply a change to a non-existing file should conflict (so it won't automatically commit), and then you can use

git rm path/to/file1

and then commit.

More generally, you could use git cherr-pick -n to ensure that no commit is made even in cases where the patch doesn't conflict, then make whatever edits you want before committing.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52