Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the differences between two branches?

Tags:

git

git-diff

I have two branches: branch_1 and branch_2.

How can I see the differences between them?

like image 997
isuruanu Avatar asked Mar 23 '12 05:03

isuruanu


People also ask

How do I see differences between branches in github?

On the Github, go to the Source view of your project. You will see a link named 'Branch List'. Once the page opens you can see a list of all the remote branches. Hit on the Compare button in front of any of the available branches to see the difference between two branches.

How do you compare two branches and codes?

To add some details on usage, the way I found to compare branches in Git Lens is to; Open the Explorer view (Ctrl + Shift + E), find the Git Lens group, right click the branch you want to compare and select 'Select for Compare',then right click the second branch and select 'Compare with Selected'.

What is the command to check diff in git?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged .


2 Answers

You want to use git diff.

git diff [<options>] <commit>..​<commit> [--] [<path>…​] 

Where <commit> is your branch name, the hash of a commit or a shorthand symbolic reference

For instance git diff abc123..def567 or git diff HEAD..origin/master

That will produce the diff between the tips of the two branches. If you'd prefer to find the diff from their common ancestor to test, you can use three dots instead of two:

git diff <commit>...<commit> 

And if you just want to check which files differ, not how the content differs, use --name-only:

git diff --name-only <commit>..​<commit> 

Note that in the <commit>..<commit> (two dot) syntax, the dots are optional; the following is synonymous:

git diff commit1 commit2 
like image 189
Lazy Badger Avatar answered Sep 24 '22 21:09

Lazy Badger


It's very simple. You just go to one branch (e.g. main is your branch).

Run the command

git checkout main git diff branch2 
like image 26
Neeraj Kumar Avatar answered Sep 22 '22 21:09

Neeraj Kumar