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

How do I get all the commit SHA ID from a branch locally and on remote?

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

我只知道 git ls-remote <git-repo-url> | awk '{print }' 给出了所有 branches/heads 的最新提交 sha。

my objective 用于查找我从 git rev-parse <branch_name> 获得的本地最新提交是否存在于从远程检索的所有提交列表中。如果是这样,我需要变基,如果远程上的最新提交 SHA 出现在本地提交 sha 列表中,但不是我的最新提交,这意味着我的本地分支已经向前推进,我不需要做变基,只需推送。

如果您要做的是确定您的本地分支是否与远程分支 分歧,我认为 git-logtriple-dot notation。例如:

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

将为您提供 local-branchremote-branch 不同 .

中的提交列表

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

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

其中 < 表示提交在 左侧而不是右侧 ,而 > 表示 在右侧但不在左边.

编辑: 除非你正在编写脚本,在这种情况下 git-merge-base with the --is-ancestor option suggested in @axiac's 是更好的选择,因为你可以只检查退出代码。