git - 如何从本地和远程分支获取所有提交 SHA ID?

标签 git git-commit git-log

我正在尝试获取特定分支的所有提交 ID 的列表,

我只知道git ls-remote <git-repo-url> | awk '{print $1}'给出所有分支/头的最新提交 sha。

我的目标是查找我从 git rev-parse <branch_name> 获得的本地最新提交存在于从远程检索的所有提交的列表中。如果是这样,我需要 rebase ,如果远程上的最新提交 SHA 存在于本地提交 SHA 列表中,但不是我的最新版本,这意味着我的本地分支已向前移动,我不需要进行 rebase ,而只需推送。

最佳答案

如果您想要做的是确定您的本地分支是否与远程分支分歧,我认为 git-log 可以为您提供更好的服务。和 triple-dot notation 。例如:

git log <local-branch>...<remote-branch> --oneline

将为您提供 local-branch 中的提交列表。或remote-branch不是两者

如果您还添加--left-right ,您将得到提交位于哪一侧的指示:

git log <local-branch>...<remote-branch> --oneline --left-right

哪里<表示提交位于左侧而不是右侧,而 >表示在右侧但不在左侧

编辑:除非您正在编写脚本,在这种情况下 git-merge-base --is-ancestor @axiac中建议的选项的comment是一个更好的选择,因为您可以只检查退出代码。

关于git - 如何从本地和远程分支获取所有提交 SHA ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50834445/

相关文章:

git - 为什么我得到 "Commit failed with error: pathspec ... did not match any file(s)"?

git - 如何显示带有分支名称的git日志

linux - ubuntu 上 ssh 的公钥存储在哪里?

java - 为什么 GIT 中的文本行结尾不统一,尽管 * text=auto

r - 撤消 Rstudio 中太大而无法推送的 git commit

intellij-idea - IntelliJ IDEA - git log 中作者姓名后的星号

git - 如何在 Git 日志中包含当前文件夹名称

windows - git log --grep 在 Windows 中不起作用

git - Netbeans:COMMIT [HEAD] 不存在

Git 子模块添加 : "a git directory is found locally" issue