我如何查看我所做的任何尚未推送到远程存储库的本地提交?有时,git状态会显示我的分支是在origin/master之前提交的X,但并不总是这样。

这是我安装Git的bug,还是我遗漏了什么?


当前回答

如果你有git子模块。。。

无论您是使用gitcherry-v还是gitlogs@{u}-p、 不要忘记通过git子模块foreach--递归“gitlogs@{u}..”。

我使用以下bash脚本来检查所有这些:

    unpushedCommitsCmd="git log @{u}.."; # Source: https://stackoverflow.com/a/8182309

    # check if there are unpushed changes
    if [ -n "$($getGitUnpushedCommits)" ]; then # Check Source: https://stackoverflow.com/a/12137501
        echo "You have unpushed changes.  Push them first!"
        $getGitUnpushedCommits;
        exit 2
    fi

    unpushedInSubmodules="git submodule foreach --recursive --quiet ${unpushedCommitsCmd}"; # Source: https://stackoverflow.com/a/24548122
    # check if there are unpushed changes in submodules
    if [ -n "$($unpushedInSubmodules)" ]; then
        echo "You have unpushed changes in submodules.  Push them first!"
        git submodule foreach --recursive ${unpushedCommitsCmd} # not "--quiet" this time, to display details
        exit 2
    fi

其他回答

gitbranch-v将显示每个本地分支是否“超前”。

你可以试试。。。。

gitk

我知道这不是一个纯粹的命令行选项,但如果你已经安装了它,并且在GUI系统上,这是一个很好的方法,可以准确地看到你想要的东西,还有更多。

(事实上,我有点惊讶,到目前为止没有人提到它。)

您可以使用git-log执行此操作:

git log origin/master..

这假设origin是上游远程的名称,master是上游分支的名称。在..之后删除任何修订名称。。暗示HEAD,它列出了尚未推送的新提交。

我使用以下别名仅获取已提交但尚未推送(针对当前分支)的文件列表(以及状态)

git config --global alias.unpushed \
"diff origin/$(git name-rev --name-only HEAD)..HEAD --name-status"

那么只需执行以下操作:

git unpushed

我认为最典型的方法是运行以下内容:

git cherry --abbrev=7 -v @{upstream}

然而,我个人更喜欢跑步:

git log --graph --decorate --pretty=oneline --abbrev-commit --all @{upstream}^..

它显示了所有未在上游合并的分支的提交,加上上游的最后一次提交(它显示为所有其他提交的根节点)。我经常使用它,所以我为它创建了别名noup。

git config --global alias.noup \
'log --graph --decorate --pretty=oneline --abbrev-commit --all @{upstream}^..'