git create commit from diff between two branches

I have two branches which have very little similar history, but are related to each other.

I want the changes between those two in one git commit.

files have been deleted and created between those patches and I want the patch to reflect that

i.e.: the following stuff will not work:

git diff branch_a branch_b -- > patchfile
git checkout branch_b
git apply patchfile # deletes and adds are ignored
git commit # we miss the deletes

A simple way to make "the diff from branch_b..branch_a" into a commit is:

  1. create and checkout branch tmp at branch_a (git branch tmp branch_a && git checkout tmp) (or git reset --hard branch_a on an existing branch)
  2. git reset --soft branch_b
  3. git commit

that commit will include all the diff between branch_b and branch_a.


This works because

  • 1. causes the files to reflect branch_a. This is the "end result" you want for the branch
  • 2. “resets the head to branch_b” but “leaves all your changed files [i.e. branch_a head] as "Changes to be committed", as git status would put it.” ←(git reset --soft docs, with this example's branches added)

If you have two branches:

  1. has-changes
  2. needs-changes

And you want to move the changes from has-changes onto needs-changes, then do the following:

git checkout -b deleteme has-changes # Create temporary branch to build commit on
git reset --soft needs-changes       # Move diff into index
git commit                           # Create the diff patch commit
git checkout needs-changes           # Switch to branch that needs changes
git cherry-pick deleteme             # Apply the diff to the needs-changes
git branch -D deleteme               # Delete the temporary branch