How to apply diff between commits to current head in git?
Use git cherry-pick --no-commit <commit>...
Then you will get all the changes in the listed commits applied on the top of your branch, and you can commit at your leisure.
If you have a more complicated use case, you might want to try git imerge
Its creator made an introduction blog post and a presentation about it. Both a bit old unfortunately.
The answer already given is fine when it's consecutive commits you want to grab.
However, if what you want is a general answer for a way to apply the diff between ANY two chosen commits (as I did when I found this page), then it's not so useful.
In my case I did the following:
To give names to things, let's say I wanted to apply the diff from commit1
to commit2
onto branch1
as a single commit.
Note that commit1
could be an ancestor or a "descendant" of commit2
, or neither.
So I do:
git status # Ensure working directory is clean, preferably no untracked files
git checkout -b temp-branch commit2
git reset --soft commit1
git commit -m "Everything from commit1 to commit2 as one commit"
git checkout branch1
git cherry-pick temp-branch
# Confirm result is what you expect
git branch -D temp-branch