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

255,435

Solution 1

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

Solution 2

You can easily do that with

git log master..branch-X

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

Solution 3

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

Solution 4

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]

Solution 5

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
Share:
255,435
Son of the Wai-Pan
Author by

Son of the Wai-Pan

Updated on July 08, 2022

Comments

  • Son of the Wai-Pan
    Son of the Wai-Pan almost 2 years

    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.

  • Pablo Fernandez heelhook
    Pablo Fernandez heelhook over 11 years
    That won't show you differences between branches though which is what is being asked.
  • Pablo Fernandez heelhook
    Pablo Fernandez heelhook over 11 years
    git log --oneline --graph --all --decorate --abbrev-commit will give you a similar output in a shorter/more readable command
  • Son of the Wai-Pan
    Son of the Wai-Pan over 11 years
    I like this: git log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset'
  • Shawn Erquhart
    Shawn Erquhart about 9 years
    Extremely overcomplicated.
  • Dave
    Dave almost 9 years
    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.
  • avmohan
    avmohan over 8 years
    git log --oneline --graph --all --decorate is enough, --abbrev-commit is not required, --oneline is short for --pretty=oneline --abbrev-commit
  • cp.engr
    cp.engr over 8 years
    @ShawnErquhart, you could alias as Dave suggests, or you could add the alias to your ~/.gitconfig.
  • cp.engr
    cp.engr over 8 years
    @Avery, the --all switch in yours shows more than just the difference requested.
  • Elliott Slaughter
    Elliott Slaughter over 8 years
    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.
  • Dave
    Dave about 8 years
    If you've already switched to branch-X you can use git log master..
  • Xavier T.
    Xavier T. almost 8 years
    @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.
  • jterm
    jterm about 7 years
    Its really only half the answer. Any commits in master that cause the branches to diverge won't show up
  • pkamb
    pkamb over 6 years
    Also, creating a Pull Request will also show the branch differences.
  • Tuffwer
    Tuffwer over 6 years
    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..
  • Ashutosh Chamoli
    Ashutosh Chamoli about 5 years
    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-X
  • claudiu.f.marginean
    claudiu.f.marginean almost 5 years
    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-merges
  • ilmirons
    ilmirons almost 5 years
    Is this different from git master..branch-X?
  • mmaruska
    mmaruska almost 5 years
    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.
  • opticyclic
    opticyclic over 4 years
    Instead of using --oneline and piping to cut you can use git log --format='%s'
  • noamgot
    noamgot almost 4 years
    Similarly to @Dave 's suggestion - if you're on branch mater then do git log ..branc-X
  • abcd
    abcd over 3 years
    the --decorate flag is no longer necessary either, as of version 2.13
  • Diogo Cardoso
    Diogo Cardoso about 3 years
    Outputs the SHA1 of every commit, prefixed with - for commits that have an equivalent in master, and + for commits that do not.
  • larsl
    larsl about 3 years
    This works especially well with patch-based workflows where commits are often cherry-picked and applied to other branches.
  • Peter Out
    Peter Out about 3 years
    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.
  • JHBonarius
    JHBonarius almost 3 years
    THIS!!! THANKS! Else I get "unknown revision or path not in the working tree."
  • alper
    alper almost 3 years
    Can gitk open in dark-mode?
  • Franco Gil
    Franco Gil over 2 years
    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.
  • pushkarajthorat
    pushkarajthorat over 2 years
    just invert the colors of the window