How do I see the commit differences between branches in git?
Asked Answered
S

11

513

I'm on branch-X and have added a couple more commits on top of it. I want to see all the differences between MASTER and the branch that I am on in terms of commits. I could just do a

git checkout master
git log

and then a

git checkout branch-X
git log

and visually diff these, but I'm hoping for an easier, less error-prone method.

Spit answered 20/12, 2012 at 4:36 Comment(2)
Possibly related: Using Git, show all commits that are in one branch, but not the other(s).Proffer
Would this statement be correct regarding answers on this page? (Most use git log.) A commit may be listed as if it were absent from one branch & present in the other even when both branches contain identical changes. Why? Git log relies on shas, which are computed using information that is volatile in normal operations (tree, timestamp, etc.). Example: cherry-picking a commit will create a new sha even though the changes within the commit are unaltered. While the OP asks specifically about "commits", most people care about "changes", a meaningful distinction. Git log may give misleading info.Anselme
A
412

You can get a really nice, visual output of how your branches differ with this

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative master..branch-X
Arita answered 20/12, 2012 at 4:43 Comment(12)
That won't show you differences between branches though which is what is being asked.Dad
git log --oneline --graph --all --decorate --abbrev-commit will give you a similar output in a shorter/more readable commandDad
I like this: git log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset'Spit
Extremely overcomplicated.Transmigrate
do this: alias diff-branches="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative". Then do this diff-branches master..branch-X. Now it's not complicated.Stegall
git log --oneline --graph --all --decorate is enough, --abbrev-commit is not required, --oneline is short for --pretty=oneline --abbrev-commitSarita
@ShawnErquhart, you could alias as Dave suggests, or you could add the alias to your ~/.gitconfig.Nocuous
@Avery, the --all switch in yours shows more than just the difference requested.Nocuous
Using double quotes instead of single like below works for me git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset" --abbrev-commit --date=relative master..branch-XSuccotash
the --decorate flag is no longer necessary either, as of version 2.13Charmainecharmane
Git log shows you what commits are in branch a that are not in branch b. Thus the order in which you supply the branches dictates the output. This is totally different to what git diff does and as such does not answer the question.Torray
%Cgreen(%cr)%Creset is the same as --date=relative, only one is needed. Furthermore, %cr does not respect --date= option, for that %cd should be used instead.Wheelsman
D
943

You can easily do that with

git log master..branch-X

That will show you commits that branch-X has but master doesn't.

Dad answered 20/12, 2012 at 4:42 Comment(6)
Is there an option if both branches contain commits that the other doesn't? Right now, you have to flip the arguments and run it both ways to see commits the other branch doesn't contain.Weirdo
If you've already switched to branch-X you can use git log master..Stegall
@ElliottSlaughter: If you want to find commit that are either in master or branch-X but not both, you can use git log master...branch-X (three dots instead of two). See man gitrevisionsfor more info.Kvass
Its really only half the answer. Any commits in master that cause the branches to diverge won't show upCuprite
If commits have been cherry-picked from branch-X to master this won't filter them out. They will still be on the list of commits "In branch-X but not on master" even though they are actually in both..Moquette
Similarly to @Stegall 's suggestion - if you're on branch mater then do git log ..branc-XChatav
A
412

You can get a really nice, visual output of how your branches differ with this

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative master..branch-X
Arita answered 20/12, 2012 at 4:43 Comment(12)
That won't show you differences between branches though which is what is being asked.Dad
git log --oneline --graph --all --decorate --abbrev-commit will give you a similar output in a shorter/more readable commandDad
I like this: git log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset'Spit
Extremely overcomplicated.Transmigrate
do this: alias diff-branches="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative". Then do this diff-branches master..branch-X. Now it's not complicated.Stegall
git log --oneline --graph --all --decorate is enough, --abbrev-commit is not required, --oneline is short for --pretty=oneline --abbrev-commitSarita
@ShawnErquhart, you could alias as Dave suggests, or you could add the alias to your ~/.gitconfig.Nocuous
@Avery, the --all switch in yours shows more than just the difference requested.Nocuous
Using double quotes instead of single like below works for me git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset" --abbrev-commit --date=relative master..branch-XSuccotash
the --decorate flag is no longer necessary either, as of version 2.13Charmainecharmane
Git log shows you what commits are in branch a that are not in branch b. Thus the order in which you supply the branches dictates the output. This is totally different to what git diff does and as such does not answer the question.Torray
%Cgreen(%cr)%Creset is the same as --date=relative, only one is needed. Furthermore, %cr does not respect --date= option, for that %cd should be used instead.Wheelsman
P
163

I think it is matter of choice and context.I prefer to use

git log origin/master..origin/develop --oneline --no-merges

It will display commits in develop which are not in master branch.

If you want to see which files are actually modified use

git diff --stat origin/master..origin/develop --no-merges

If you don't specify arguments it will display the full diff. If you want to see visual diff, install meld on linux, or WinMerge on windows. Make sure they are the default difftools .Then use something like

git difftool -y origin/master..origin/develop --no-merges

In case you want to compare it with current branch. It is more convenient to use HEAD instead of branch name like use:

git fetch
git log origin/master..HEAD --oneline --no-merges

It will show you all the commits, about to be merged

Paramo answered 2/11, 2015 at 23:49 Comment(1)
If you compare a release branch that might have merges. You may like remove the merge commits (that do not add any value) with the help of the param --no-merges like: git log origin/master..HEAD --oneline --no-mergesShel
C
32

I'd suggest the following to see the difference "in commits". For symmetric difference, repeat the command with inverted args:

git cherry -v master [your branch, or HEAD as default]
Coryza answered 17/11, 2015 at 0:36 Comment(4)
Is this different from git master..branch-X?Valvulitis
Sure, "git cherry" is smart: it translates from "commits" into "patches/diffs" and can avoid reporting a "patch" which is on both branches but applied in different order.Coryza
Outputs the SHA1 of every commit, prefixed with - for commits that have an equivalent in master, and + for commits that do not.Breadnut
This works especially well with patch-based workflows where commits are often cherry-picked and applied to other branches.Krein
D
16

If you are on Linux, gitg is way to go to do it very quickly and graphically.

If you insist on command line you can use:

git log --oneline --decorate

To make git log nicer by default, I typically set these global preferences:

git config --global log.decorate true
git config --global log.abbrevCommit true
Demagogic answered 20/12, 2012 at 4:55 Comment(0)
V
9

if you want to use gitk:

gitk master..branch-X

it has a nice old school GUi

Venose answered 26/8, 2015 at 8:37 Comment(2)
Can gitk open in dark-mode?Maledict
just invert the colors of the windowBrusa
S
8

I used some of the answers and found one that fit my case ( make sure all tasks are in the release branch).

Other methods works as well but I found that they might add lines that I do not need, like merge commits that add no value.

git fetch
git log origin/master..origin/release-1.1 --oneline --no-merges

or you can compare your current with master

git fetch
git log origin/master..HEAD --oneline --no-merges

git fetch is there to make sure you are using updated info.

In this way each commit will be on a line and you can copy/paste that into an text editor and start comparing the tasks with the commits that will be merged.

Shel answered 24/6, 2019 at 17:53 Comment(1)
THIS!!! THANKS! Else I get "unknown revision or path not in the working tree."Fredfreda
S
7

To see all the commits like what you gonna see in "Commits" tab of your PR, run these:

1. Basic:

git log --oneline origin/base..origin/my-branch

2. If you don't want branches/tags, add --no-decorate

git log --oneline --no-decorate origin/base..origin/my-branch

3. If you want to sort commits chronologically (oldest first), add --reverse

git log --oneline --no-decorate --reverse origin/base..origin/my-branch

It took me a while to find these gorgeous. Hope it's helpful to someone.

Settlement answered 27/2, 2023 at 13:54 Comment(0)
K
5

Not the perfect answer but works better for people using Github:

enter image description here

Go to your repo: Insights -> Network

Koto answered 28/9, 2017 at 8:15 Comment(2)
Also, creating a Pull Request will also show the branch differences.Ricoricochet
This only work for Public repos., what if I have a Private repo and the admin does not provide me all the access to response the question? For this current answer is not valid.Cindiecindra
I
4

If you want to compare based on the commit messages, you can do the following:

git fetch
git log --oneline origin/master | cut -d' ' -f2- > master_log
git log --oneline origin/branch-X | cut -d' ' -f2- > branchx_log
diff <(sort master_log) <(sort branchx_log)
Inexperience answered 21/8, 2019 at 5:24 Comment(1)
Instead of using --oneline and piping to cut you can use git log --format='%s'Disentwine
L
0
#! /bin/bash
if ((2==$#)); then
  a=$1
  b=$2
  alog=$(echo $a | tr '/' '-').log
  blog=$(echo $b | tr '/' '-').log
  git log --oneline $a > $alog
  git log --oneline $b > $blog
  diff $alog $blog
fi

Contributing this because it allows a and b logs to be diff'ed visually, side by side, if you have a visual diff tool. Replace diff command at end with command to start visual diff tool.

Lazarus answered 8/10, 2016 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.