如何列出未推送的 Git 提交(本地但不在源上)

发布于 2024-08-17 04:46:51 字数 152 浏览 7 评论 0 原文

如何查看我所做的、尚未推送到远程存储库的任何本地提交?有时,git status 会打印出我的分支是在 origin/master 之前进行的 X 次提交,但并非总是如此。

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

How can I view any local commits I've made, that haven't yet been pushed to the remote repository? Occasionally, git status will print out that my branch is X commits ahead of origin/master, but not always.

Is this a bug with my install of Git, or am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

短叹 2024-08-24 04:46:52

一种方法是列出一个分支上可用但另一分支上不可用的提交。

git log ^origin/master master

one way of doing things is to list commits that are available on one branch but not another.

git log ^origin/master master
青萝楚歌 2024-08-24 04:46:52

我真的迟到了,我不确定它是什么时候实现的,但是要看看 git push 会做什么,只需使用 --dry-run 选项< /代码>:

$ git push --dry-run
To ssh://bitbucket.local.lan:7999/qarepo/controller.git
   540152d1..21bd921c  imaging -> imaging

I'm really late to the party, and I'm not sure when it was implemented, but to see what a git push would do, just use the --dry-run option:

$ git push --dry-run
To ssh://bitbucket.local.lan:7999/qarepo/controller.git
   540152d1..21bd921c  imaging -> imaging
孤者何惧 2024-08-24 04:46:52

如上所述:

git diff origin/master..HEAD

但是如果你使用的是 git gui

打开 gui 界面后,选择“Repository”->下的“可视化历史记录

注意:有些人喜欢使用 CMD 提示符/终端,而有些人喜欢使用 Git GUI(为了简单起见)

As said above:

git diff origin/master..HEAD

But if you are using git gui

After opening gui interface, Select "Repository"->Under that "Visualize History"

Note: Some people like to use CMD Prompt/Terminal while some like to use Git GUI (for simplicity)

烧了回忆取暖 2024-08-24 04:46:52

如果您有 git 子模块...

无论您是使用 gitcherry-v 还是 gitlogs@{u}..-p,都不要忘记包含您的 子模块通过
git 子模块 foreach --recursive 'git log @{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

If you have git submodules...

Whether you do git cherry -v or git logs @{u}.. -p, don't forget to include your submodules via
git submodule foreach --recursive 'git logs @{u}..'.

I am using the following bash script to check all of that:

    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
夜吻♂芭芘 2024-08-24 04:46:52

这是我的便携式解决方案(shell 脚本也可以在 Windows 上运行,无需额外安装),它显示了所有分支与原始版本的差异: /.gitconfig_files/custom_commands/git-fetch-log" rel="nofollow noreferrer">git-fetch-log

示例输出:

==== branch [behind 1]

> commit 652b883 (origin/branch)
| Author: BimbaLaszlo <[email protected]>
| Date:   2016-03-10 09:11:11 +0100
|
|     Commit on remote
|
o commit 2304667 (branch)
  Author: BimbaLaszlo <[email protected]>
  Date:   2015-08-28 13:21:13 +0200

      Commit on local

==== master [ahead 1]

< commit 280ccf8 (master)
| Author: BimbaLaszlo <[email protected]>
| Date:   2016-03-25 21:42:55 +0100
|
|     Commit on local
|
o commit 2369465 (origin/master, origin/HEAD)
  Author: BimbaLaszlo <[email protected]>
  Date:   2016-03-10 09:02:52 +0100

      Commit on remote

==== test [ahead 1, behind 1]

< commit 83a3161 (test)
| Author: BimbaLaszlo <[email protected]>
| Date:   2016-03-25 22:50:00 +0100
|
|     Diverged from remote
|
| > commit 4aafec7 (origin/test)
|/  Author: BimbaLaszlo <[email protected]>
|   Date:   2016-03-14 10:34:28 +0100
|
|       Pushed remote
|
o commit 0fccef3
  Author: BimbaLaszlo <[email protected]>
  Date:   2015-09-03 10:33:39 +0200

      Last common commit

为日志传递的参数,例如 --oneline 或 <可以使用code>--patch。

Here's my portable solution (shell script which works on Windows too without additional install) which shows the differences from origin for all branches: git-fetch-log

An example output:

==== branch [behind 1]

> commit 652b883 (origin/branch)
| Author: BimbaLaszlo <[email protected]>
| Date:   2016-03-10 09:11:11 +0100
|
|     Commit on remote
|
o commit 2304667 (branch)
  Author: BimbaLaszlo <[email protected]>
  Date:   2015-08-28 13:21:13 +0200

      Commit on local

==== master [ahead 1]

< commit 280ccf8 (master)
| Author: BimbaLaszlo <[email protected]>
| Date:   2016-03-25 21:42:55 +0100
|
|     Commit on local
|
o commit 2369465 (origin/master, origin/HEAD)
  Author: BimbaLaszlo <[email protected]>
  Date:   2016-03-10 09:02:52 +0100

      Commit on remote

==== test [ahead 1, behind 1]

< commit 83a3161 (test)
| Author: BimbaLaszlo <[email protected]>
| Date:   2016-03-25 22:50:00 +0100
|
|     Diverged from remote
|
| > commit 4aafec7 (origin/test)
|/  Author: BimbaLaszlo <[email protected]>
|   Date:   2016-03-14 10:34:28 +0100
|
|       Pushed remote
|
o commit 0fccef3
  Author: BimbaLaszlo <[email protected]>
  Date:   2015-09-03 10:33:39 +0200

      Last common commit

Parameters passed for log, e.g. --oneline or --patch can be used.

花想c 2024-08-24 04:46:52
git show

将显示本地提交中的所有差异。

git show --name-only

将显示本地提交 ID 和提交名称。

git show

will show all the diffs in your local commits.

git show --name-only

will show the local commit id and the name of commit.

水中月 2024-08-24 04:46:52
git diff origin

假设您的分支设置为跟踪来源,那么这应该会向您显示差异。

git log origin

将为您提供提交的摘要。

git diff origin

Assuming your branch is set up to track the origin, then that should show you the differences.

git log origin

Will give you a summary of the commits.

眸中客 2024-08-24 04:46:51

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

git log origin/master..HEAD

当 HEAD 位于 master 分支上时,这给出了未推送的提交的日志。


同样,要查看差异:

git diff origin/master..HEAD

This gives a log of all commits between origin/master and HEAD:

git log origin/master..HEAD

When HEAD is on the master branch, this gives a log of unpushed commits.


Similarly, to view the diff:

git diff origin/master..HEAD
陈甜 2024-08-24 04:46:51

要查看所有尚未推送的分支上的所有提交:

git log --branches --not --remotes

要查看每个分支上的最新提交以及分支名称:

git log --branches --not --remotes --simplify-by-decoration --decorate --oneline

To see all commits on all branches that have not yet been pushed:

git log --branches --not --remotes

To see the most recent commit on each branch, as well as the branch names:

git log --branches --not --remotes --simplify-by-decoration --decorate --oneline
绝影如岚 2024-08-24 04:46:51

显示您在本地但不在上游的所有提交:

git log @{u}..

@{u}@{upstream} 表示当前分支的上游分支(请参阅 git rev-parse --helpgit 帮助修订 详细信息)。

Show all commits that you have locally but not upstream with:

git log @{u}..

@{u} or @{upstream} means the upstream branch of the current branch (see git rev-parse --help or git help revisions for details).

红焚 2024-08-24 04:46:51
git log origin/master..

假设 origin 是上游远程的名称,master 是上游分支的名称。在 .. 之后省略任何修订名称意味着 HEAD,其中列出了尚未推送的新提交。 [git 日志]

git log origin/master..

This assumes that origin is the name of your upstream remote and master is the name of your upstream branch. Leaving off any revision name after .. implies HEAD, which lists the new commits that haven't been pushed. [git log]

盛夏尉蓝 2024-08-24 04:46:51

所有其他答案都谈论“上游”(您从中提取的分支)。
但是本地分支可以推送到与其拉取的不同分支。

master 可能不会推送到远程跟踪分支“origin/master”。
master上游分支可能是 origin/master,但它可以推送到远程跟踪分支 origin/xxx > 甚至anotherUpstreamRepo/yyy
这些由当前分支的branch.*.pushremote 以及全局remote.pushDefault 值一起设置。

在寻找未推送的提交时, 远程跟踪分支很重要:跟踪远程分支(其中本地分支)的位置推到。
远程分支再次可以是origin/xxx,甚至是anotherUpstreamRepo/yyy。

Git 2.5+(2015 年第二季度)为此引入了一个新的快捷方式:@{push}

请参阅提交 29bc885, 提交 3dbe9db 提交 adfe5d0提交 48c5847提交a1ad0eb提交e291c75提交 979cb24提交 1ca41a1提交 3a429d0提交a9f9f8c提交 8770e6f提交 da66b27提交f052154提交 9e3751d提交 ee2499f [全部来自 21 2015 年 5 月],并提交 e41bf35 [2015 年 5 月 1 日] 杰夫·金 (peff)
(由 Junio C Hamano -- gitster -- 合并于 提交 c4a8354,2015 年 6 月 5 日)

提交 adfe5d0 解释:

sha1_name:实现@{push}简写

在三角形工作流程中,每个分支可能有两个不同的兴趣点:您通常从中提取的 @{upstream} 以及您通常推送到的目的地。后者没有简写,但有它很有用。

例如,您可能想知道哪些提交尚未提交
尚未推送

git log @{push}..

或者作为一个更复杂的示例,假设您通常从 origin/master (您将其设置为 @{upstream})中提取更改,并将更改推送到您的分叉(例如,myfork/topic)。
您可以从多台机器推送到您的分叉,这要求您集成来自推送目的地的更改,而不是上游
使用此补丁,您可以执行以下操作:

git rebase @{push}

而不是输入全名。

提交 29bc885 添加:

for-each-ref:接受“%(push)”格式

正如我们使用“%(upstream)”来报告每个引用的“@{upstream}”一样,此补丁添加了“%(push )”来匹配“@{push}”。
它支持与上游相同的跟踪格式修饰符(因为例如,您可能想知道哪些分支已提交要推送)。

如果您想查看与您要推送的分支相比,您的本地分支领先/落后有多少提交:

git for-each-ref --format="%(refname:short) %(push:track)" refs/heads

All the other answers talk about "upstream" (the branch you pull from).
But a local branch can push to a different branch than the one it pulls from.

A master might not push to the remote-tracking branch "origin/master".
The upstream branch for master might be origin/master, but it could push to the remote tracking branch origin/xxx or even anotherUpstreamRepo/yyy.
Those are set by branch.*.pushremote for the current branch along with the global remote.pushDefault value.

It is that remote-tracking branch that counts when seeking unpushed commits: the one that tracks the branch at the remote where the local branch would be pushed to.
The branch at the remote can be, again, origin/xxx or even anotherUpstreamRepo/yyy.

Git 2.5+ (Q2 2015) introduces a new shortcut for that: <branch>@{push}

See commit 29bc885, commit 3dbe9db, commit adfe5d0, commit 48c5847, commit a1ad0eb, commit e291c75, commit 979cb24, commit 1ca41a1, commit 3a429d0, commit a9f9f8c, commit 8770e6f, commit da66b27, commit f052154, commit 9e3751d, commit ee2499f [all from 21 May 2015], and commit e41bf35 [01 May 2015] by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit c4a8354, 05 Jun 2015)

Commit adfe5d0 explains:

sha1_name: implement @{push} shorthand

In a triangular workflow, each branch may have two distinct points of interest: the @{upstream} that you normally pull from, and the destination that you normally push to. There isn't a shorthand for the latter, but it's useful to have.

For instance, you may want to know which commits you haven't
pushed yet
:

git log @{push}..

Or as a more complicated example, imagine that you normally pull changes from origin/master (which you set as your @{upstream}), and push changes to your fork (e.g., as myfork/topic).
You may push to your fork from multiple machines, requiring you to integrate the changes from the push destination, rather than upstream.
With this patch, you can just do:

git rebase @{push}

rather than typing out the full name.

Commit 29bc885 adds:

for-each-ref: accept "%(push)" format

Just as we have "%(upstream)" to report the "@{upstream}" for each ref, this patch adds "%(push)" to match "@{push}".
It supports the same tracking format modifiers as upstream (because you may want to know, for example, which branches have commits to push).

If you want to see how many commit your local branches are ahead/behind compared to the branch you are pushing to:

git for-each-ref --format="%(refname:short) %(push:track)" refs/heads
满天都是小星星 2024-08-24 04:46:51

我之前完成了一次提交,没有推送到任何分支,也没有推送到远程或本地。只是提交。其他答案对我来说没有任何作用,但是:

git reflog

我在那里找到了我的承诺。

I had a commit done previously, not pushed to any branch, nor remote nor local. Just the commit. Nothing from other answers worked for me, but with:

git reflog

There I found my commit.

短叹 2024-08-24 04:46:51

方便的 git 别名,用于在当前分支中查找未推送的提交:

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

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

Handy git alias for looking for unpushed commits in current branch:

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

What this basically does:

git log origin/branch..branch

but also determines current branch name.

花开浅夏 2024-08-24 04:46:51

你可以尝试......

gitk

我知道它不是一个纯粹的命令行选项,但如果你安装了它并且在 GUI 系统上,这是一个很好的方法来准确地看到你正在寻找的东西以及更多。

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

You could try....

gitk

I know it is not a pure command line option but if you have it installed and are on a GUI system it's a great way to see exactly what you are looking for plus a whole lot more.

(I'm actually kind of surprised no one mentioned it so far.)

这个俗人 2024-08-24 04:46:51

git Branch -v 将显示每个本地分支是否“领先”。

git branch -v will show, for each local branch, whether it's "ahead" or not.

酒与心事 2024-08-24 04:46:51

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

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

然后执行以下操作:

git unpushed

I use the following alias to get just the list of files (and the status) that have been committed but haven't been pushed (for the current branch)

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

then just do:

git unpushed
幸福丶如此 2024-08-24 04:46:51

我相信最典型的方法是运行类似的命令:

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}^..'

I believe the most typical way of doing this is to run something like:

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

However, I personally prefer running:

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

which shows the commits from all branches which are not merged upstream, plus the last commit in upstream (which shows up as a root node for all the other commits). I use it so often that I have created alias noup for it.

git config --global alias.noup \
'log --graph --decorate --pretty=oneline --abbrev-commit --all @{upstream}^..'
谈场末日恋爱 2024-08-24 04:46:51
git cherry -v

这将列出您本地的评论历史记录(尚未推送)以及相应的消息

git cherry -v

This will list out your local comment history (not yet pushed) with corresponding message

愁以何悠 2024-08-24 04:46:51

我建议你去看脚本 https://github.com/badele/gitcheck,我已经编码了这个用于检查一次的脚本会遍历所有 git 存储库,并显示谁未提交以及谁未推送/拉取。

这是示例结果 在此处输入图像描述

I suggest you go see the script https://github.com/badele/gitcheck, i have coded this script for check in one pass all your git repositories, and it show who has not commited and who has not pushed/pulled.

Here a sample result enter image description here

瑾夏年华 2024-08-24 04:46:51

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

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

git fetch

这是 100% 安全的,不会模拟您的工作副本。如果有更改,git status 将显示X commits before origin/master

您现在可以显示远程但不在本地的提交日志:

git log HEAD..origin

It is not a bug. What you probably seeing is git status after a failed auto-merge where the changes from the remote are fetched but not yet merged.

To see the commits between local repo and remote do this:

git fetch

This is 100% safe and will not mock up your working copy. If there were changes git status wil show X commits ahead of origin/master.

You can now show log of commits that are in the remote but not in the local:

git log HEAD..origin
你怎么敢 2024-08-24 04:46:51

这对我来说效果更好:

git log --oneline @{upstream}..

或者:

git log --oneline origin/(remotebranch)..

This worked better for me:

git log --oneline @{upstream}..

or:

git log --oneline origin/(remotebranch)..
才能让你更想念 2024-08-24 04:46:51

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

$ easy_install --user unpushed

或者

$ 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)

请参阅 unpushed --help官方说明更多信息。它还具有一个 cronjob 脚本 unpushed-notify,用于在屏幕上通知未提交和未推送的更改。

There is tool named unpushed that scans all Git, Mercurial and Subversion repos in specified working directory and shows list of ucommited files and unpushed commits.
Installation is simple under Linux:

$ easy_install --user unpushed

or

$ sudo easy_install unpushed

to install system-wide.

Usage is simple too:

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

See unpushed --help or official description for more information. It also has a cronjob script unpushed-notify for on-screen notification of uncommited and unpushed changes.

余罪 2024-08-24 04:46:51

要轻松列出所有分支中的所有未推送的提交,您可以使用以下命令:

 git log --branches  @{u}..

To list all unpushed commit in all branches easily you can use this command:

 git log --branches  @{u}..
來不及說愛妳 2024-08-24 04:46:51

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

$ git checkout

git 通过告诉您相对于您的来源“领先 N 提交”来进行响应。因此,现在查看日志时请记住该数字。如果您“领先 3 个提交”,则历史记录中的前 3 个提交仍然是私有的。

If the number of commits that have not been pushed out is a single-digit number, which it often is, the easiest way is:

$ git checkout

git responds by telling you that you are "ahead N commits" relative your origin. So now just keep that number in mind when viewing logs. If you're "ahead by 3 commits", the top 3 commits in the history are still private.

ペ泪落弦音 2024-08-24 04:46:51

类似:查看未合并的分支:

git branch --all --no-merged

这些可能值得怀疑,但我推荐 cxreg 的答案

Similar: To view unmerged branches:

git branch --all --no-merged

Those can be suspect but I recommend the answer by cxreg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文