user1491250
user1491250

Reputation: 1961

git: How to view commits to a single branch

A colleague created a local branch ('branchA') from master, did some work, pushed it, merged in master, did some more work and pushed it again. Concurrently, other colleagues have been working on other branches and merging them to master.

Now I need to pull branchA to review it. So I've done a git pull and git checkout -b branchA origin/branchA which is fine. But all of the commands (git diff/log/show) show commits made across all branches in the repo.

How can I view a diff of all of the commits made to branchA against the version of master that it was created from?

Also how can I git diff branchA against the current HEAD of master, but only view files changed within branchA?

Upvotes: 19

Views: 18142

Answers (5)

clearwater82
clearwater82

Reputation: 1776

git log HEAD..branch when you are on the master branch.

Look here for more detail: How to get the changes on a branch in git

Upvotes: 0

Karthik Bose
Karthik Bose

Reputation: 36104

  1. git diff master..brnachA: will compare all modified files between HEAD of master and branchA.
  2. git diff master...brnachA: will compare branchA against the version of master that it was created from.

FYI: git diff will generate output in command line. If you want to see the output in some visual tools, use git difftool.

You can pass all git diff arguments and options to git difftool as well.

Upvotes: 4

Christopher
Christopher

Reputation: 44284

The following applies to your second question, how to find the differences between branchA and your local's current version of master. You want to use 'double dot' syntax with git log to see all the commits in branchA that aren't in master. To wit:

git log master..branchA

Per the git log man page:

SYNOPSIS
   git log [<options>] [<since>..<until>] [[--] <path>...]
   ...
   <since>..<until>
   Show only commits between the named two commits. When either <since> or <until> is omitted, it defaults to HEAD, i.e. the tip of the current branch.
   For a more complete list of ways to spell <since> and <until>, see gitrevisions(7).

If you'd like to see commits in either master or branchA, but not in both, you can use 'triple-dot' syntax:

git log master...branchA

Finally, you can use the exact same syntax with git diff, namely, git diff master..branchA and git diff master...branchA, respectively.

As an aside, if you have branchA checked out, you don't even need to specify it as <until>. Git will assume HEAD if it's left unspecified, so these two sets of commands are equivalent:

git checkout branchA
git log master..

and

git log master..branchA

Upvotes: 21

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72815

Git commits don't retain information about "which branch" they were committed on. They only give you a point in the tree you can walk back from. Once a merge happens, you have no way, from a merge commit, to decide which parent came from the branch you started from. This is discussed in some details over here.

This is my understanding. I'd love to be corrected if I'm wrong.

Upvotes: 2

forvaidya
forvaidya

Reputation: 3315

you can use git log --branches=mybranch

This will show log on specific branch

Upvotes: 0

Related Questions