How can I check with the command line the latest commit hash of a particular Git branch?
-
1Have you tried using 'git log -n 1' when you've checked out that specific branch? – Rick van Bodegraven Mar 28 '13 at 08:46
-
git log gives local repositories hash , but git hub repository has new hash . – mbdvg Mar 28 '13 at 08:48
-
2`git log -n 1 [branch_name]` branch_name(may be remote or local branch) is optional. Without branch_name it will show the latest commit of current branch. – Rahul Tapali Mar 28 '13 at 10:16
-
See also [here](http://stackoverflow.com/questions/949314/how-to-retrieve-the-hash-for-the-current-commit-in-git). – Raphael Mar 21 '15 at 12:34
-
4Use: `git rev-parse [branch_name]` – Datageek Dec 13 '16 at 17:38
-
1How would this great question be closed by 'off topic' ? – Nam G VU Jul 22 '17 at 06:36
7 Answers
git log -n 1 [branch_name]
branch_name
(may be remote or local branch) is optional. Without branch_name
, it will show the latest commit on the current branch.
For example:
git log -n 1
git log -n 1 origin/master
git log -n 1 some_local_branch
git log -n 1 --pretty=format:"%H" #To get only hash value of commit

- 4,979
- 12
- 37
- 45

- 9,887
- 7
- 31
- 44
-
11Get short hash: 'git rev-parse --short --verify my_branch' from similar question: http://stackoverflow.com/a/949391/134761 – angularsen Jan 19 '15 at 08:00
-
Nice, I added this to my .gitconfig so that I can just copy the last commit with `git hash | pbcopy clip` ``` [alias] hash = "!f(){\ git log -n 1 --pretty=format:"%H";\ }; f" ``` – Pants Apr 12 '19 at 15:30
-
@Jaro After running this command, git log -n 1 --pretty=format:"%H", I get the commit id perfectly fine. I have a question, how can I store this commit-id in a variable in a batch script? – Shreya Maheshwari Mar 24 '22 at 07:24
-
Never mind, I found the solution. https://stackoverflow.com/questions/37004019/how-do-i-get-the-output-from-a-call-to-git-into-a-variable-in-a-batch-script – Shreya Maheshwari Mar 24 '22 at 07:48
Use git ls-remote git://github.com/<user>/<project>.git
. For example, my trac-backlog project gives:
:: git ls-remote git://github.com/jszakmeister/trac-backlog.git
5d6a3c973c254378738bdbc85d72f14aefa316a0 HEAD
4652257768acef90b9af560295b02d0ac6e7702c refs/heads/0.1.x
35af07bc99c7527b84e11a8632bfb396823326f3 refs/heads/0.2.x
5d6a3c973c254378738bdbc85d72f14aefa316a0 refs/heads/master
520dcebff52506682d6822ade0188d4622eb41d1 refs/pull/11/head
6b2c1ed650a7ff693ecd8ab1cb5c124ba32866a2 refs/pull/11/merge
51088b60d66b68a565080eb56dbbc5f8c97c1400 refs/pull/12/head
127c468826c0c77e26a5da4d40ae3a61e00c0726 refs/pull/12/merge
2401b5537224fe4176f2a134ee93005a6263cf24 refs/pull/15/head
8aa9aedc0e3a0d43ddfeaf0b971d0ae3a23d57b3 refs/pull/15/merge
d96aed93c94f97d328fc57588e61a7ec52a05c69 refs/pull/7/head
f7c1e8dabdbeca9f9060de24da4560abc76e77cd refs/pull/7/merge
aa8a935f084a6e1c66aa939b47b9a5567c4e25f5 refs/pull/8/head
cd258b82cc499d84165ea8d7a23faa46f0f2f125 refs/pull/8/merge
c10a73a8b0c1809fcb3a1f49bdc1a6487927483d refs/tags/0.1.0
a39dad9a1268f7df256ba78f1166308563544af1 refs/tags/0.2.0
2d559cf785816afd69c3cb768413c4f6ca574708 refs/tags/0.2.1
434170523d5f8aad05dc5cf86c2a326908cf3f57 refs/tags/0.2.2
d2dfe40cb78ddc66e6865dcd2e76d6bc2291d44c refs/tags/0.3.0
9db35263a15dcdfbc19ed0a1f7a9e29a40507070 refs/tags/0.3.0^{}
Just grep for the one you need and cut it out:
:: git ls-remote git://github.com/jszakmeister/trac-backlog.git | \
grep refs/heads/master | cut -f 1
5d6a3c973c254378738bdbc85d72f14aefa316a0
Or, you can specify which refs you want on the command line and avoid the grep with:
:: git ls-remote git://github.com/jszakmeister/trac-backlog.git refs/heads/master | \
cut -f 1
5d6a3c973c254378738bdbc85d72f14aefa316a0
Note: it doesn't have to be the git://
URL. It could be https://
or git@github.com:
too.
Originally, this was geared towards finding out the latest commit of a remote branch (not just from your last fetch, but the actual latest commit in the branch on the remote repository). If you need the commit hash for something locally, the best answer is:
git rev-parse branch-name
It's fast, easy, and a single command. If you want the commit hash for the current branch, you can look at HEAD:
git rev-parse HEAD

- 44,691
- 9
- 89
- 79
-
5
-
`git rev-parse my_branch_name` OR `git log -n 1 my_branch_name | grep commit | awk '{ print $2 }'` simpler answer; will output only the last commit hash of `my_branch_name` – betoharres Jul 18 '19 at 02:51
-
@betoharres the OP was originally trying to determine the commit on a remote repository (not what we have in `refs/remotes`, but the actual remote repo). `git rev-parse branch-name` is definitely the way to go for a local branch. – John Szakmeister Jul 18 '19 at 08:24
-
This only works in the field of the `git svn` because `git svn fetch` changes the `git log` output and so the `git log` no longer can be used to get the last *remote* git repository commit hash. – Andry Sep 30 '19 at 11:18
-
@Andry I'm not sure what you mean... I'm having trouble following your comment. Can you explain more? – John Szakmeister Sep 30 '19 at 22:20
-
@JohnSzakmeister I meant this the only which works for the `git svn`, because for example, `git svn fetch` changes the output of the `git log` too. – Andry Sep 30 '19 at 23:22
-
@Andry I guess that's my issue, I expect both `git rev-parse` and `git log` to work the same, just that `rev-parse` is more direct. – John Szakmeister Oct 01 '19 at 08:20
-
@JohnSzakmeister For me that was the case where the `git log` was affected by other commands and I could not use it in the first place. – Andry Oct 01 '19 at 14:34
Try using git log -n 1
after doing a git checkout branchname
. This shows the commit hash, author, date and commit message for the latest commit.
Perform a git pull origin/branchname
first, to make sure your local repo matches upstream.
If perhaps you would only like to see a list of the commits your local branch is behind on the remote branch do this:
git fetch origin
git cherry localbranch remotebranch
This will list all the hashes of the commits that you have not yet merged into your local branch.

- 67
- 6

- 179
- 9
-
my requirement is to check local repositories and github repositories commit hash are same or not , how can we do this ? – mbdvg Mar 28 '13 at 09:02
-
As far as I know, there is no way the commit hashes could differ between your local and your remote repo's for the same commits. Did you perhaps mean a list of the commits your local repository is missing versus the remote repository? – Rick van Bodegraven Mar 28 '13 at 09:05
-
Yes Rick van , there is a mismatch ,so , i want to show that there is a difference in local and github repo , So need a way to find out – mbdvg Mar 28 '13 at 09:09
you can git fetch nameofremoterepo
, then git log
and personally, I alias gitlog
to git log --graph --oneline --pretty --decorate --all
. try out and see if it fits you
-
-
`--online` is a shorthand for `--pretty=oneline --abbrev-commit`. But you are using `--pretty` as well, which reverts `--pretty=oneline`... I would recommend `git log --graph --abbrev-commit --decorate --all` instead. – Felix Kling Mar 28 '13 at 09:00
-
Adding to John Szakmeister's excellent answer above (https://stackoverflow.com/a/15679887/4597676), you could do this without touching the mouse by piping the output of git rev-parse BRANCHNAME
to a command like pbcopy
or similar:
git rev-parse HEAD | pbcopy
.
you can then pbpaste, or paste it using your mouse.- It looks like on a linux machine you should be able to use
xsel
or similar. (i have not tried this myself). https://askubuntu.com/q/1157488. - it looks like you can use clip.exe on Windows from the clipboard. cf: https://www.winhelponline.com/articles/248/1/Copy-command-line-output-directly-to-the-Clipboard-using-Clipexe.html

- 788
- 1
- 9
- 18
In a comment you wrote
i want to show that there is a difference in local and github repo
As already mentioned in another answer, you should do a git fetch origin
first. Then, if the remote is ahead of your current branch, you can list all commits between your local branch and the remote with
git log master..origin/master --stat
If your local branch is ahead:
git log origin/master..master --stat
--stat
shows a list of changed files as well.
If you want to explicitly list the additions and deletions, use git diff
:
git diff master origin/master

- 795,719
- 175
- 1,089
- 1,143
Note that when using "git log -n 1 [branch_name]" option. -n returns only one line of log but order in which this is returned is not guaranteed. Following is extract from git-log man page
.....
.....
Commit Limiting
Besides specifying a range of commits that should be listed using the special notations explained in the description, additional commit limiting may be applied.
Using more options generally further limits the output (e.g. --since=<date1> limits to commits newer than <date1>, and using it with --grep=<pattern> further limits to commits whose log message has a line that matches <pattern>), unless otherwise noted.
Note that these are applied before commit ordering and formatting options, such as --reverse.
-<number>
-n <number>
.....
.....

- 1