Search code examples
gitgit-log

Git log rev range master gives different results between master and branch


I'm on a release branch, where I apply cherry-picked commits from master (main development).

I wanted to check whether some old commits have already been copied over so I started using git log and grep to find one of the commit hashes.

When on a branch I usually compare latest commits there with respect to master with:

git log --oneline -20

and

git log --oneline -20 master

But now that I wanted to check a range of commits, after some trials and errors I ended up using:

git log --oneline <myhash>~..HEAD master

Which, when master is checked out, gives the expected results - the list of commits from <myhash> (inclusive) to the latest one of master.

If ran when I'm on a different branch, though, that same command yields different results (actually it seems to give all the commits from the beginning of the branch up to the latest): why?


Solution

  • If you want to see all commits on a branch starting from a specific other commit, use the following:

    git log --oneline start..branch
    

    Where start is the starting commit and branch is the final commit. Git will show all commits reachable from branch and exclude those reachable from start (same behavior is achieved by: git log ^start branch, i.e. "not start" and "branch")

    When you do start..HEAD master you will get all commits between start and the current commit/branch AND all commits reachable from master. In that case you are actually specifying 3 commits: ^start, HEAD, and master. Git will then traverse the graph to give you the result: all commits reachable from HEAD (the current commit/branch), all commits reachable from master, but exclude all commits reachable from start