git 如何知道对 master 的最新提交是否已推送到远程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21989388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:46:44  来源:igfitidea点击:

How to know if latest commit to master has been pushed to the remote?

gitgithubmergebranchcommit

提问by Michael Durrant

I have merged a branch in to master and now I can see that in my git log

我已经将一个分支合并到 master 中,现在我可以在我的 git log

Some time has passed and now I want to know whether I previously also pushed master (with that commit) to the remote. How can I tell if it has been pushed?

一段时间过去了,现在我想知道我之前是否也将 master (通过该提交)推送到远程。我怎么知道它是否被推送了?

I can think of a few workaround such as recloning the repository elsewhere, or resetting and checking and then re-merging but I feel that there's probably a somewhat simpler answer.

我可以想到一些解决方法,例如在其他地方重新克隆存储库,或者重置和检查然后重新合并,但我觉得可能有一个更简单的答案。

fyi this is different from How can I know in git if a branch has been already merged into master?as I know it has been merged, just don't know about the remote push.

仅供参考,这与如何在 git 中知道分支是否已合并到 master 中不同?据我所知,它已合并,只是不知道远程推送。

回答by Klas Mellbourn

Do

> git status

If the output is

如果输出是

# On branch master
nothing to commit, working directory clean

Then you have pushed the current commit.

然后你已经推送了当前的提交。

If the output instead begins with

如果输出改为以

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)

Then you have a local commit that has not yet been pushed. You see this because the remote branch, origin/master, points to the commit that was last pushed to origin. However, your branch is ahead of 'origin/master', meaning that you have a local commit that has been created after the last pushed commit.

然后你有一个尚未推送的本地提交。您会看到这一点,因为远程分支origin/master指向上次推送到源的提交。但是,您的分支是ahead of 'origin/master',这意味着您有一个在上次推送提交后创建的本地提交。

If the commit you are interested in is not the latest, then you can do

如果您感兴趣的提交不是最新的,那么您可以这样做

> git log --decorate --oneline

to find out if the commit in question is before or after the commit pointed to by origin/master.
If the commit is after (higher up in the log than) origin/master, then it has not been pushed.

找出有问题的提交是在 指向的提交之前还是之后origin/master
如果提交在 (在日志中高于) 之后origin/master,则它没有被推送。

回答by somesh

If you have made multiple commits and not sure which one of them have been pushed to remote, try this:

如果您进行了多次提交并且不确定其中哪一个已被推送到远程,请尝试以下操作:

git log origin/<remote-branch>..<local-branch>

Example:

例子:

git log origin/master..master

This would list out all commits in your local branch that have not been pushed to the remote branch mentioned.

这将列出您本地分支中尚未推送到提到的远程分支的所有提交。

回答by VasiliNovikov

A solution to do it programmatically:

以编程方式执行此操作的解决方案:

git merge-base --is-ancestor @{u} HEAD

You may also want to check if your local directory is clean, e.g. there are no uncommitted file changes:

您可能还想检查您的本地目录是否干净,例如没有未提交的文件更改:

test -z "$(git status --porcelain)"

回答by asd

I suggest you run this:

我建议你运行这个:

$ git fetch --all
Fetching origin
Fetching upstream

This will fetch the latest data from all remotes.

这将从所有遥控器获取最新数据。

Then you run:

然后你运行:

$ git branch -v
  master       ef762af [ahead 3] added attach methods
* testing      4634e21 added -p flag
  upstream     1234567 [ahead 1, behind 7] updated README.md

This will show which branches you're ahead or behind on.

这将显示您在哪些分支上领先或落后。

I posted this because none of the other answers mention fetching the remote data, that step is crucial.

我发布这个是因为其他答案都没有提到获取远程数据,这一步至关重要。

回答by Vlad Nikitin

you can use git log --graph --all --decorate, it will show where each ref is located (HEAD, master, origin/master etc.)

您可以使用git log --graph --all --decorate,它将显示每个 ref 的位置(HEAD、master、origin/master 等)

回答by crea1

Try this. git branch -r --contains <sha1>

尝试这个。 git branch -r --contains <sha1>

For a commit in my repository I can see it exists on the remote develop branch

对于我的存储库中的提交,我可以看到它存在于远程开发分支上

git branch  -r --contains 7914e54ea7e30c7f446e791df66bd3a5805c978a
origin/develop