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

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


当前回答

如果未推出的提交数量是一个位数,通常是一个,那么最简单的方法是:

$ git checkout

git的回应是告诉你,相对于你的起源,你“领先N个提交”。所以现在在查看日志时,请记住这个数字。如果您“领先3次提交”,则历史上前3次提交仍然是私有的。

其他回答

在当前分支中查找未推送提交的便捷数字别名:

alias unpushed = !GIT_CURRENT_BRANCH=$(git name-rev --name-only HEAD) && git log origin/$GIT_CURRENT_BRANCH..$GIT_CURRENT_BRANCH --oneline

它的基本作用是:

git log origin/branch..branch

而且还确定当前分支名称。

这不是一个bug。您可能看到的是自动合并失败后的git状态,其中从远程获取更改但尚未合并。

要查看本地回购和远程回购之间的提交,请执行以下操作:

git fetch

这是100%安全的,不会模拟您的工作副本。如果有更改,git状态将显示X在原始/主之前提交。

现在,您可以显示远程而非本地的提交日志:

git log HEAD..origin

这给出了origin/master和HEAD之间所有提交的日志:

git log origin/master..HEAD

当HEAD位于主分支上时,这将提供未推送提交的日志。


同样,要查看差异:

git diff origin/master..HEAD

如果你有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

有一个名为unpushed的工具,它扫描指定工作目录中的所有Git、Mercurial和Subversion repo,并显示未提交文件和未提交提交的列表。在Linux下安装很简单:

$ easy_install --user unpushed

or

$ sudo easy_install unpushed

以在系统范围内安装。

用法也很简单:

$ unpushed ~/workspace
* /home/nailgun/workspace/unpushed uncommitted (Git)
* /home/nailgun/workspace/unpushed:master unpushed (Git)
* /home/nailgun/workspace/python:new-syntax unpushed (Git)

有关详细信息,请参阅未推送的帮助或官方说明。它还有一个cronjob脚本unpushed notify,用于在屏幕上通知未提交和未推送的更改。