102

I did following (I simplified this comparing to a reality):

  • created a branch Branch1, switched to it
  • added file File1 and modified existing file File2 and commited this
  • figured out that I don't need File1, removed it and commited this

So, the actual difference between original branch and Branch1 is only modification of File2.

I want to get this actual difference between branches and put in Branch2. Generally speaking, I want to get rid of not necessary history of adding/removing File1.

Victor Ronin
  • 22,758
  • 18
  • 92
  • 184

3 Answers3

216

Let's assume that you started on the branch master. Then you can do:

git diff master Branch1 > ../patchfile
git checkout Branch2    
git apply ../patchfile

Alternatively, if your goal is to rewrite history, then you could use an interactive rebase to squash commits.

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • I don't think "interactive rebase" can solve his problem. With rebase you can pick which commits are you want, but in his case, he has 2 commits but neither of these 2 is he want. – sevenever Mar 20 '17 at 08:11
  • 3
    @sevenever If, in the interactive rebase, he squashes together the two commits, the addition and removal of **File1** will disappear and only the modification of **File2** will remain. That would achieve what he wants. – Klas Mellbourn Mar 20 '17 at 11:58
  • 2
    As an addendum, if you're using a setup where the diff is automatically colored, add `--no-color` to `diff` to ensure the resulting patch is still valid. If you need to include binary files, add `--binary`. – Etheryte May 15 '22 at 22:23
12

This is a simple git diff

git diff --name-only SHA1 SHA2

Where SHA1/2 are the hashes of the 2 commits at the top of each branch.

Or you can do

git diff --name-only develop...

To compare your branch against the develop branch

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
MaxK
  • 424
  • 3
  • 9
  • 1
    This gives me only the names of changed files (which is nice). However, I need more than just names to create Branch2 – Victor Ronin May 21 '13 at 17:23
  • 2
    I think more correct solution would be "git diff SHA1 SHA2 >some.patch" and after on new branch "patch -p1 < some.patch" – Victor Ronin May 21 '13 at 17:48
  • This is exactly what I was looking for! Allows me to restore all my changes as local changes on a base branch so I can see them all at once in my IDE rather than losing the changes every time I commit. Thanks! – B T Dec 02 '21 at 20:29
0

I would do an interactive rebase on HEAD~2 and squash the last two commits together. Assuming that you want to keep the history as is in Branch1 and simplify it in Branch2, do the following (current branch Branch1):

git checkout -b Branch2
git rebase -i 'HEAD~2'

An editor will open up, showing something like

pick 1b58da0 Added File1, changed File2
pick d3f4f51 Delete File1

and many explanatory comments how rebasing works. Change the last commit to a squash and close the editor.

pick 1b58da0 Added File1, changed File2
squash d3f4f51 Delete File1

A new editor will open up where you can specify the new commit message. It would probably now just read

Changed File2

Close it and you're done, both commits are squashed together on Branch2 and Branch1 retains your original history. Note that if you don't need to retain the original history, you can just skip checking out Branch2 and work directly on Branch1. Only do that if you haven't published your last two commits on Branch1 already.

sebastian
  • 2,386
  • 26
  • 22