如何列出未推送的 Git 提交(本地但不在源上)
如何查看我所做的、尚未推送到远程存储库的任何本地提交?有时,git status
会打印出我的分支是在 origin/master
之前进行的 X 次提交,但并非总是如此。
这是我安装的 Git 的错误,还是我遗漏了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
一种方法是列出一个分支上可用但另一分支上不可用的提交。
one way of doing things is to list commits that are available on one branch but not another.
我真的迟到了,我不确定它是什么时候实现的,但是要看看 git push 会做什么,只需使用
--dry-run 选项< /代码>:
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 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)
如果您有 git 子模块...
无论您是使用
gitcherry-v
还是gitlogs@{u}..-p
,都不要忘记包含您的 子模块通过git 子模块 foreach --recursive 'git log @{u}..'
。我正在使用以下 bash 脚本来检查所有这些:
If you have git submodules...
Whether you do
git cherry -v
orgit logs @{u}.. -p
, don't forget to include your submodules viagit submodule foreach --recursive 'git logs @{u}..'
.I am using the following bash script to check all of that:
这是我的便携式解决方案(shell 脚本也可以在 Windows 上运行,无需额外安装),它显示了所有分支与原始版本的差异: /.gitconfig_files/custom_commands/git-fetch-log" rel="nofollow noreferrer">git-fetch-log
示例输出:
为日志传递的参数,例如
--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:
Parameters passed for log, e.g.
--oneline
or--patch
can be used.将显示本地提交中的所有差异。
将显示本地提交 ID 和提交名称。
will show all the diffs in your local commits.
will show the local commit id and the name of commit.
假设您的分支设置为跟踪来源,那么这应该会向您显示差异。
将为您提供提交的摘要。
Assuming your branch is set up to track the origin, then that should show you the differences.
Will give you a summary of the commits.
这给出了 origin/master 和 HEAD 之间所有提交的日志:
当 HEAD 位于 master 分支上时,这给出了未推送的提交的日志。
同样,要查看差异:
This gives a log of all commits between origin/master and HEAD:
When HEAD is on the master branch, this gives a log of unpushed commits.
Similarly, to view the diff:
要查看所有尚未推送的分支上的所有提交:
要查看每个分支上的最新提交以及分支名称:
To see all commits on all branches that have not yet been pushed:
To see the most recent commit on each branch, as well as the branch names:
显示您在本地但不在上游的所有提交:
@{u}
或@{upstream}
表示当前分支的上游分支(请参阅git rev-parse --help
或git 帮助修订
详细信息)。Show all commits that you have locally but not upstream with:
@{u}
or@{upstream}
means the upstream branch of the current branch (seegit rev-parse --help
orgit help revisions
for details).取自: Git:查看所有未推送的提交或不在另一个分支中的提交。
Taken from: Git: See all unpushed commits or commits that are not in another branch.
假设
origin
是上游远程的名称,master
是上游分支的名称。在..
之后省略任何修订名称意味着HEAD
,其中列出了尚未推送的新提交。 [git 日志
]This assumes that
origin
is the name of your upstream remote andmaster
is the name of your upstream branch. Leaving off any revision name after..
impliesHEAD
, which lists the new commits that haven't been pushed. [git log
]所有其他答案都谈论“上游”(您从中提取的分支)。
但是本地分支可以推送到与其拉取的不同分支。
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 解释:
提交 29bc885 添加:
如果您想查看与您要推送的分支相比,您的本地分支领先/落后有多少提交:
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 beorigin/master
, but it could push to the remote tracking branchorigin/xxx
or evenanotherUpstreamRepo/yyy
.Those are set by
branch.*.pushremote
for the current branch along with theglobal 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 evenanotherUpstreamRepo/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:
Commit 29bc885 adds:
If you want to see how many commit your local branches are ahead/behind compared to the branch you are pushing to:
我之前完成了一次提交,没有推送到任何分支,也没有推送到远程或本地。只是提交。其他答案对我来说没有任何作用,但是:
我在那里找到了我的承诺。
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:
There I found my commit.
方便的 git 别名,用于在当前分支中查找未推送的提交:
这基本上是做什么的:
而且还确定当前分支名称。
Handy git alias for looking for unpushed commits in current branch:
What this basically does:
but also determines current branch name.
你可以尝试......
我知道它不是一个纯粹的命令行选项,但如果你安装了它并且在 GUI 系统上,这是一个很好的方法来准确地看到你正在寻找的东西以及更多。
(事实上,到目前为止还没有人提到过,我有点惊讶。)
You could try....
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.)
git Branch -v
将显示每个本地分支是否“领先”。git branch -v
will show, for each local branch, whether it's "ahead" or not.我使用以下别名来获取已提交但尚未推送(对于当前分支)的文件列表(和状态),
然后执行以下操作:
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)
then just do:
我相信最典型的方法是运行类似的命令:
但是,我个人更喜欢运行:
它显示来自未合并到上游的所有分支的提交,加上最后一次提交上游(它显示为所有其他提交的根节点)。我经常使用它,因此我为它创建了别名
noup
。I believe the most typical way of doing this is to run something like:
However, I personally prefer running:
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.这将列出您本地的评论历史记录(尚未推送)以及相应的消息
This will list out your local comment history (not yet pushed) with corresponding message
我建议你去看脚本 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
这不是一个错误。您可能看到的是自动合并失败后的 git status,其中从远程获取更改但尚未合并。
要查看本地存储库和远程存储库之间的提交,请执行以下操作:
这是 100% 安全的,不会模拟您的工作副本。如果有更改,
git status
将显示X commits before origin/master
。您现在可以显示远程但不在本地的提交日志:
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:
This is 100% safe and will not mock up your working copy. If there were changes
git status
wil showX commits ahead of origin/master
.You can now show log of commits that are in the remote but not in the local:
这对我来说效果更好:
或者:
This worked better for me:
or:
有一个名为 unpushed 的工具,可以扫描指定工作目录中的所有 Git、Mercurial 和 Subversion 存储库并显示列表未提交的文件和未推送的提交。
Linux 下的安装很简单:
或者
在系统范围内安装。
用法也很简单:
请参阅
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:
or
to install system-wide.
Usage is simple too:
See
unpushed --help
or official description for more information. It also has a cronjob scriptunpushed-notify
for on-screen notification of uncommited and unpushed changes.要轻松列出所有分支中的所有未推送的提交,您可以使用以下命令:
To list all unpushed commit in all branches easily you can use this command:
如果尚未推出的提交数量是个位数(通常是个位数),那么最简单的方法是:
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 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.
类似:查看未合并的分支:
这些可能值得怀疑,但我推荐 cxreg 的答案
Similar: To view unmerged branches:
Those can be suspect but I recommend the answer by cxreg