161

I'm on a branch with some changes. Changing branch is a pain as some files are locked by processes, so to change branch I'd have to stop all the processes which have locks, then stash the changes before checking out the other branch to see its log.

Is it possible to view the log for a different branch, without having to check it out?

2
  • 1
    Have your tried git log <branch>, where <branch> stands for the name of the branch of interest?
    – jub0bs
    Jul 12, 2015 at 13:50
  • 2
    @Jubobs, no, I didn't. I should just try the obvious before resorting to searching the internet and finding exotic suggestions around cherry and rev-list.
    – BanksySan
    Jul 12, 2015 at 13:55

2 Answers 2

234

TL;DR

Use

git log <branch>

where <branch> is the name of the branch of interest.

From the git-log man-page...

A simplified version of the git-log synopsis given in that command's man page is

git log [<revision range>]

Further down, you can find the following passage:

When no <revision range> is specified, it defaults to HEAD (i.e. the whole history leading to the current commit)

In others words, git log is equivalent to git log HEAD. If you're on a branch, called mybranch, say, this command is also equivalent to git log mybranch.

You want to limit the log to commits reachable from another branch, i.e. a branch you're not currently on. The easiest way to do that is to explicitly pass the name of the branch of interest to git log:

git log <branchname>

See the gitrevisions manpage for more details about the many forms that the <revision-range> argument can take.

3
  • 1
    It seems like you must have the branch to be locally checked out first to run "git log" like this. At least git 2.27.0 behaves like this.
    – a_girl
    Oct 5, 2020 at 14:39
  • 1
    @a_girl Not sure what you mean by "locally checked out" here. You can run this on a local branch (e.g. master), whether it be checked out or not, and on a remote branch (e.g. origin/master).
    – jub0bs
    Oct 5, 2020 at 15:31
  • 4
    ooooohhh, now I got it. I tried git log mybranch and it throw an error, but then, when I run git checkout mybranch; git checkout master; git log mybranch it worked even without "origin/". That's why I thought that I need to checkout my branch first. But I simply had to use "git log origin/mybranch" in order to look for a history of the remote branch. Thank you.
    – a_girl
    Oct 5, 2020 at 19:07
2

This is not an answer but maybe a helpful note to the answer:

Recently I got

fatal: ambiguous argument 'other_branch_name': both revision and filename
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

If logging for a single file is wanted than synopsis should be

git log other_branch_name -- relative_path_to_file

at least if the file relative_path_to_file does not exist in the other branch

I did not check if it applies for directories also but I expect the synopsis to be

git log other_branch_name -- relative_path_to_dir/

Not the answer you're looking for? Browse other questions tagged or ask your own question.