Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the difference between two branches in Git

Tags:

git

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.

like image 728
Victor Ronin Avatar asked May 21 '13 17:05

Victor Ronin


People also ask

How do I compare two GitHub branches on desktop?

To compare the current branch/commit, press Ctrl+Shift+B or choose the option shown in the image below & then select the branch &/or commit to compare. Bonus: To compare in GitHub web, press Ctrl+Shift+C. It only shows differing commits, and if you click on a commit it will show the changed of that commit.

How do I compare two branches in git Sourcetree?

Another way to do this is to right-click on a branch and select the "Diff against current" context menu command (current refers to the branch you are currently working on). This will give you the diff between the head commits of the two branches.


1 Answers

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.

like image 55
Klas Mellbourn Avatar answered Oct 22 '22 00:10

Klas Mellbourn