fgalan
fgalan

Reputation: 12322

Get hashes of commit that are behind/ahead remote branch in git

It usually happens that local branch is behind/ahead remote branch and git reports about it:

$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 3 commits.
  (use "git push" to publish your local commits)

However, is there any simple way of knowning with exact commits are these? I mean, the 3 commit hashes that are ahead origin in the above example.

Thanks!

Upvotes: 0

Views: 185

Answers (1)

Chris
Chris

Reputation: 137205

There are a few ways to do this, but I'd do git log --oneline origin/develop..develop. This should output something like

1234abc Commit message
abcd123 Some other commit message
1a2b3c4 And yet another message

origin/develop..develop means "all commits in the develop branch that aren't in the origin/develop branch". Think of it like "differences starting from the first branch and going to the second branch".

If you're behind, you can do the opposite: git log --oneline develop..origin/develop.

And if you really only want the hashes, instead of --oneline you can use something like --pretty="%h". This will output something like

1234abc
abcd123
1a2b3c4

You can learn more about ^, .. and ... in revisions from the documentation:

You can think of this as a set operation. Commits given on the command line form a set of commits that are reachable from any of them, and then commits reachable from any of the ones given with ^ in front are subtracted from that set. The remaining commits are what comes out in the command’s output. Various other options and paths parameters can be used to further limit the result.

Thus, the following command:

$ git rev-list foo bar ^baz

means "list all the commits which are reachable from foo or bar, but not from baz".

A special notation <commit1>..<commit2> can be used as a short-hand for ^'<commit1>' <commit2>. For example, either of the following may be used interchangeably:

$ git rev-list origin..HEAD
$ git rev-list HEAD ^origin

Upvotes: 1

Related Questions