0

I would like to preserve my current branch's history, but add a new commit which changes the state of the branch to match another branch — essentially overwriting or resetting the files in my current working directory to the state they have in the other branch, so that I can then create a new commit. How do I do this?

I can't do a cherry-pick because that would try to apply the diff between the specified commit and it's parent onto the current HEAD.

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80

1 Answers1

0

Instead of thinking about 'resetting' your working directory to the state of some other branch/commit, you only need to create a diff between your current branch and the target branch, then apply the diff.

You can do this in a single porcelain command, when on the branch you want to append the commit onto and in the top-level directory of that repository, with git checkout --patch SHA-or-branch-name .

Then choose which hunks to apply or not apply, and do a git commit.

Alternatively, apply all hunks with git diff SHA-or-branch-name | git apply - and commit it.

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80