命令从分支获取最新的 Git 提交散列

如何使用命令行检查特定 Git 分支的最新提交散列?

164847 次浏览

在执行 git checkout branchname之后尝试使用 git log -n 1。这将显示最新提交的提交散列、作者、日期和提交消息。

首先执行 git pull origin/branchname,以确保您的本地回购匹配上游。

如果您只想看到远程分支上本地分支落后的提交列表,那么可以这样做:

git fetch origin
git cherry localbranch remotebranch

这将列出所有尚未合并到本地分支中的提交散列。

你可以 git fetch nameofremoterepo,然后 git log

就我个人而言,我化名为 gitloggit log --graph --oneline --pretty --decorate --all。试试看它是否适合你

In a comment you wrote

我想说明的是,在本地回购和 Github 回购之间是有区别的

正如在另一个答案中已经提到的,您应该首先执行 git fetch origin。然后,如果远程在当前分支之前,您可以列出本地分支和远程之间的所有提交

git log master..origin/master --stat

如果你的本地分公司领先:

git log origin/master..master --stat

--stat shows a list of changed files as well.

如果要显式列出添加和删除,请使用 git diff:

git diff master origin/master
git log -n 1 [branch_name]

branch_name(可以是远程分支或本地分支)是可选的。如果没有 branch_name,它将显示当前分支上的最新提交。

例如:

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

使用 git ls-remote git://github.com/<user>/<project>.git。例如,我的 trac-backlog 项目给出:

:: 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^{}

只要找到你需要的,然后剪掉它:

:: git ls-remote git://github.com/jszakmeister/trac-backlog.git | \
grep refs/heads/master | cut -f 1
5d6a3c973c254378738bdbc85d72f14aefa316a0

或者,您可以在命令行上指定需要哪些引用,并使用以下命令避免 grep:

:: git ls-remote git://github.com/jszakmeister/trac-backlog.git refs/heads/master | \
cut -f 1
5d6a3c973c254378738bdbc85d72f14aefa316a0

注意: 它不一定是 git://的 URL。它也可以是 https://或者 git@github.com:

最初,这是为了查找远程分支的最新提交(不仅仅是从最近一次获取中,还包括远程存储库中分支中的实际最新提交)。如果您需要本地的提交散列,最佳答案是:

git rev-parse branch-name

它快速,简单,只需一个命令。如果需要当前分支的提交散列,可以查看 HEAD:

git rev-parse HEAD

请注意,在使用“ git log-n 1[ Branch _ name ]”选项时。- n 只返回一行日志,但不保证返回的顺序。 下面是从 git-log 手册页中提取的内容

.....
.....
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>
.....
.....

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: