Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply diff between commits to current head in git?

I was working on some topic branch on project I participate in. I normally rebase my branches before making a pull request. This time however, due to changes in master, rebasing is a big pain. Lot of conflicts and, what's worse, after manual merging commits look just ugly and loose their point. I would definitely prefer to obtain diff between my branch's head and latest commit present on both branches, then apply this diff to master's head and finally create brand new shiny commits with git add -p. So my question is: how to achieve it? One possibility would be to simply merge master into my topic branch, but is there a more elegant solution? I believe yes.

Thanks for your help.

EDIT:

One file has been moved on master branch and my commits are altering this file (using it's old name).

like image 997
skalee Avatar asked Nov 26 '12 12:11

skalee


2 Answers

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.

like image 56
Christoffer Hammarström Avatar answered Oct 21 '22 03:10

Christoffer Hammarström


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
like image 20
Wildcard Avatar answered Oct 21 '22 04:10

Wildcard