70

Let's say I have a Git repo with branch A and branch B.
Branch B is currently checked out (i.e. .git/HEAD contains refs/heads/B).
Now, without checking out branch A, how to view the history (log) of path/file in branch A?

Following git help rev-parse, I've attempted to use git log A:path/file, but that doesn't seem to work (outputs nothing). Why?

pattivacek
  • 5,617
  • 5
  • 48
  • 62
ulidtko
  • 14,740
  • 10
  • 56
  • 88

1 Answers1

108

Try this (without the angle brackets):

git log <branch> -- <path/to/file>

pattivacek
  • 5,617
  • 5
  • 48
  • 62
  • Good, this works. Now, explaining in plumbing terms, what's the difference? – ulidtko Sep 16 '13 at 14:43
  • 1
    I'm not familiar with the `:` syntax. I'm just going off of `man git-log`, which offers `git log [] [] [[--] ...]`. Similarly, mistyping many commands yields this warning from git: `Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'` – pattivacek Sep 16 '13 at 14:46
  • Does http://stackoverflow.com/questions/18085344/git-parameters-colon-vs-forward-slash help answer your question? – pattivacek Sep 16 '13 at 14:53
  • It looks like the colon (`:`) is used to separate two distinct trees (e.g. a remote branch and a local branch), which isn't happening here. You are only examining one branch, and to separate branch from path, you need to use the fairly standard `--`. This provides some explanation: http://git-scm.com/book/ch9-5.html – pattivacek Sep 16 '13 at 15:02
  • Doesn't really help, unfortunately. It is confusing to remember different syntaxes for seemingly the same thing: `git show A:path/file`, bug `git log A -- path/file`?.. `git diff A:path/file B:path/file`, but `git blame A -- path/file`? I have to look this up each time I need it. – ulidtko Sep 16 '13 at 15:05
  • 3
    @ulidtko: The mnemonics I use is that `:` i.e. *one* expression is about *one* object, i.e. tree (directory) or a file, neither of which has history, so it is good for `git show A:path/file`. `git log -- ` is *two* expressions: history starting from `` limited to commits that touch ``. HTH – Jakub Narębski Sep 17 '13 at 14:57
  • Great; been looking for this! Generalizing on this, you can also see the list of all files in that branch with `git log --name-only` option or see the actual changes with `git log -p`. – JESii Apr 28 '17 at 14:45