flivan
flivan

Reputation: 471

Create new branch that contains diff of directory between 2 branches

I have a project with multiple branches. I can see the commits that exist on branch2, but not on branch1 with this command:

git log branch1..branch2 path/to/dir

I would like to create a new branch off of branch1 containing the commits displayed when the above command is executed.

For instance:

On branch1:

git log path/to/dir
  commit1
  commit2

On branch2:

git log path/to/dir
  commit1
  commit2
  commit3
  commit4    

Then the command I mentioned above would return the following:

git log branch1..branch2 path/to/dir
  commit3
  commit4

I would like to find a way to create a new branch with its base off of branch1, let's call it integration-branch that contains those 2 commits.

git log is probably not the correct first step here, but it helps me illustrate what I want to accomplish. I presume there is and advanced form of git merge that allows partial merging of a branch into another.

Another option for me would be to first create the integration-branch off of branch1, then, from branch2 merge only the changes of path/to/dir

Upvotes: 0

Views: 265

Answers (1)

twalberg
twalberg

Reputation: 62439

If I understand you correctly, you want to create a new branch that starts at branch1 and contains only the commits from branch2 that touch path/to/dir. The best way to do this would be to use git cherry-pick. Here's how I would do it:

// create a new branch to work on
$ git checkout -b newbranch branch1
// grab the commit IDs we want and cherry-pick them
$ git cherry-pick $(git log --pretty=format:%H --reverse branch1..branch2 -- path/to/dir)

There's a potential for conflicts in files other than path/to/dir if commits that are not being selected attempt to introduce changes to those files that violate the preconditions of the patch because intervening commits have been skipped. Getting around that would require going through the interesting commits, generating patches limited to just the changes made to interesting files, and applying those manually in sequence, which could get somewhat tedious if we're talking about lots of commits here...

Upvotes: 1

Related Questions