从分支获取最新 Git 提交哈希的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15677439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command to get latest Git commit hash from a branch
提问by mbdvg
How can I check with the command line the latest commit hash of a particular Git branch?
如何使用命令行检查特定 Git 分支的最新提交哈希?
采纳答案by John Szakmeister
Use git ls-remote git://github.com/<user>/<project>.git
. For example, my trac-backlog project gives:
使用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^{}
Just grep for the one you need and cut it out:
只需为您需要的那个 grep 并将其剪掉:
:: 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:
或者,您可以在命令行上指定您想要的引用并避免使用 grep:
:: 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 [email protected]:
too.
注意:它不必是git://
URL。可能是https://
,[email protected]:
也可能是。
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:
它快速、简单且只需一个命令。如果你想要当前分支的提交哈希,你可以查看 HEAD:
git rev-parse HEAD
回答by Rahul Tapali
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.
branch_name
(可能是远程或本地分支)是可选的。如果没有branch_name
,它将显示当前分支上的最新提交。
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
回答by Rick van Bodegraven
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.
git log -n 1
做完之后尝试使用git checkout branchname
。这显示了最新提交的提交哈希、作者、日期和提交消息。
Perform a git pull origin/branchname
first, to make sure your local repo matches upstream.
git pull origin/branchname
首先执行,以确保您的本地存储库与上游匹配。
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.
这将列出您尚未合并到本地分支的提交的所有哈希值。
回答by nglinh
you can git fetch nameofremoterepo
, then git log
你可以git fetch nameofremoterepo
,然后git log
and personally, I alias gitlog
to git log --graph --oneline --pretty --decorate --all
. try out and see if it fits you
就个人而言,我别名gitlog
为git log --graph --oneline --pretty --decorate --all
. 试试看是否适合你
回答by Felix Kling
In a comment you wrote
在你写的评论中
i want to show that there is a difference in local and github repo
我想表明本地和 github 存储库存在差异
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 fetch origin
。然后,如果远程在您当前的分支之前,您可以列出本地分支和远程之间的所有提交
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.
--stat
还显示了已更改文件的列表。
If you want to explicitly list the additions and deletions, use git diff
:
如果要明确列出添加和删除,请使用git diff
:
git diff master origin/master
回答by bhav22
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
请注意,使用“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>
.....
.....