How can I see the differences between two branches?
GitGit DiffGit Problem Overview
I have two branches: branch_1
and branch_2
.
How can I see the differences between them?
Git Solutions
Solution 1 - Git
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
Solution 2 - Git
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
Solution 3 - Git
Code is simply git diff master..develop
Options:
- You can add
--name-only
to only see the names of the files. - If you want to see the changes of specific files or folders. Then add
-- folderOrFileName
at the end. - If you want to compare the local branch with the remote one, then
fetch --all
to fetch all remote branches, and rungit diff --name-only [branchName]..origin/[branchName]
. For examplegit diff --name-only develop..origin/develop
Solution 4 - Git
There are many different ways to compare branches, and it's depend on the specific use case you need.
Many times you want to compare because something broken and you want to see what has been changes, then fix it, and see again what changed before commiting.
Personally when I want to see the diff what I like to do:
git checkout branch_1 # checkout the oldest branch
git checkout -b compare-branch # create a new branch
git merge --no-commit --squash branch_2 # put files from the new branch in the working folder
git status # see file names that changes
git diff # see the content that changed.
Using this solution you will see the diff, you can also see only the file names using git status
, and the most important part you will be able to execute branch_2
while seeing the diff (branch_2 is on the working tree). If something had broken you can editing the files and fix it. Anytime, you can type again git status
or git diff
to see the diff from the new edit to branch_a
.
Solution 5 - Git
You can simply show difference by-
git diff b1...b2
Or you can show commit difference using-
git log b1..b2
You can see commit difference in a nice graphical way using -
git log --oneline --graph --decorate --abbrev-commit b1..b2
Solution 6 - Git
In Eclipse(J2EE version) , open "Window --> Show view --> Git Repository". if you have checked out 2 local git branches for examples then you will have bunch of branches in Local section. select any 2 git local branches and do " right click and select "Compare with each other in Tree menu".
Open view "Git Tree Compare" and u will be able to see side by side diff for all files.