How do I see the differences between two branches?

Asked 2023-09-20 20:21:45 View 400,042

How do I see the differences between branches branch_1 and branch_2?

  • You want something different from the straightforward git diff branch_1 branch_2? (Note, if the names branch_1 and branch_2 also name files, you need git diff branch_1 branch_2 --) - anyone
  • stackoverflow.com/questions/822811/differences-in-git-branches - anyone
  • The cited duplicate does not answer the question... Determining which files have changed with git diff --name-status master..branchName is markedly different than seeing the exact differences between branches with something like git diff branch_1 branch_2. Or maybe I'm missing something obvious... - anyone
  • Not only is the "duplicate" a different question, this question is the number one google match for "git diff two branches". - anyone
  • git difftool branch..otherBranch lets you SEE the differences in the Visual tool that you choose. e.g. Meld. This is the answer. - anyone

Answers

Use git diff.

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

<commit> is a branch name, a commit hash, or a shorthand symbolic reference.

Examples: git diff abc123..def567, 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>

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

Answered   2023-09-20 20:21:45

  • The same syntax works for comparing a branch with a tag or a tag with another tag. - anyone
  • Note that you can also add a file or folder name after the above two commands. - anyone
  • @chiyachaiya your explanation helped me but git diff b1...b2 is not same as git diff b2...b1. For example once we started b2 from b1 and when if we make some changes to b1, git diff b2...b1 will show changes made to b1 after b2 started. If we do git diff b1...b2 it will give changes made to b2 which are not in b1. - anyone
  • If you get fatal: bad revision 'some-branch' then this is probably a remote branch. You probably need something like git diff remotes/origin/some-branch my-local-branch - anyone
  • git diff ..branch_2 compares the checked out branch to branch_2 - anyone

Go to a branch (e.g. main), then run diff against another branch (e.g. branch2):

git checkout main
git diff branch2

Answered   2023-09-20 20:21:45

  • this didn't work for me, it showed no changes. I only had local commits though. - anyone
  • Try preceding the branch name with the remote name, e.g. origin/ - anyone
git diff master..develop

Options:

  • Add --name-only to only see the names of the files.
  • Add -- folderOrFileName at the end to see the changes of specific files or folders.
  • To compare the local branch with the remote one, then run git fetch --all to fetch all remote branches, and run:
    git diff --name-only [branchName]..origin/[branchName]
    
    Example: git diff --name-only develop..origin/develop.

Answered   2023-09-20 20:21:45

  • master may be main now for your repo - anyone

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

Answered   2023-09-20 20:21:45

  • I didn't know about git log b1 b2, thanks! - anyone
  • personally I like the graph. It looks awesome when you work in multiple branches and merge them. - anyone

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.

Answered   2023-09-20 20:21:45

There are two ways to see the differences between two branches.The modifications that have been made to the files in each branch will be shown by these commands.

  1. Use the git diff command to view the differences between two branches in a Git repository.

    git diff branch1 branch2 will show all the differences.

    If you wish to compare a specific file between the two branches, you can use this command as:

    git diff branch1 branch2 path/to/file

  2. The git log command can also be used to view the differences between two branches. Run the git log command with the —left-right parameter and the two branches you wish to compare like this:

    git log --left-right branch1...branch2

Answered   2023-09-20 20:21:45

  • The second option is exactly what I was looking for. This is the perfect one-liner for a quick diff by commit. For an even quicker look. to refer to the current branch's position use HEAD, and to narrow to just commit messages add the aptly named --oneliner option. For example: git log --left-right --oneline HEAD...branch2 - anyone

Sometimes it's nice to see the diff as a tree...

git difftool --dir-diff branch..otherBranch

or to compare a remote branch to the local workspace...

git difftool --dir-diff origin/branch .

For example, when bitbucket decides for performance reasons it will only show you a "three way merge" diff rather than the actual complete differences between the two branches you've selected.

This will show the diff as a tree in the tool you've selected. e.g. in meld.

Inspired by @GregRundlett comment.

Answered   2023-09-20 20:21:45

  • I had neglected to link to the damning admission that they've removed a key diff capability to optimize their performance ('cost?'). - anyone

When on the feature branch, merge your target branch and then run a diff against it. For example, if you want to see what changes your feature branch add to master, do the following:

// Fetch from all remotes
git fetch

// Check out your feature branch
git checkout feature

// Merge origin/master to your branch
git merge origin/master

// Compare to origin/master
git diff origin/master

Answered   2023-09-20 20:21:45

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.

Answered   2023-09-20 20:21:45