git - How can I see the differences between two branches?

ID : 257

viewed : 116

Tags : gitgit-diffgit





Top 5 Answer for git - How can I see the differences between two branches?

vote vote

98

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> 
vote vote

88

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 
vote vote

74

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.

vote vote

69

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

vote vote

56

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 run git diff --name-only [branchName]..origin/[branchName]. For example git diff --name-only develop..origin/develop

Top 3 video Explaining git - How can I see the differences between two branches?







Related QUESTION?