【问题标题】:Why does a git fetch following a git pull clear Your branch is ahead of 'origin/master' message?为什么在 git pull clear 之后 git fetch 你的分支在“origin/master”消息之前?
【发布时间】:2025-12-11 04:45:01
【问题描述】:

我的问题是为什么@Rich 在这个 SO post 中的答案有效?

我正在运行 git 版本 git 版本 1.7.1 并拥有一个裸 git 存储库,并通过将开发更改提交并推送到远程 ics_client.git 然后 git pull 来更新开发和生产环境到生产系统。

将更改拉到生产环境后,我看到如下输出(在以下示例中,我尝试了git fetch,后跟git merge FETCH_HEAD,但得到的消息与git pull 相同。

[ics@bucky ics_client]$ git fetch origin master
gituser@h2oamr's password: 
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 7 (delta 5), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From h2oamr:ics_client
 * branch            master     -> FETCH_HEAD
[ics@bucky ics_client]$ git merge FETCH_HEAD
Updating 59a2f6a..05f8d8b
Fast-forward
 Reports.mf     |    5 +-
 fgiusr.c       |  354 ++++++++++++++++++++++++++++----------------------------
 rangebatch.4gl |   52 +++++++-
 3 files changed, 225 insertions(+), 186 deletions(-)
[ics@bucky ics_client]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)

那么,这是 git 的正常部分吗?难道我做错了什么?

【问题讨论】:

    标签: git


    【解决方案1】:

    git fetch 后跟git merge 执行的操作与git pull 非常相似。但是,合并操作也会产生它自己的更改(因此您的远程本地表示现在在前面),下面的 fetch 操作通过同步远程分支的本地表示来处理这些更改。

    在低于 1.8.4 的 git 版本中,当您 git pull 时,FETCH_HEAD 会更新并合并到您签出的 HEAD 中,但不会更新远程存储库的本地表示。因此,虽然它说您领先于远程分支,但它们实际上是彼此排成一行的。

    【讨论】:

    • 请注意,OP 的“合并”是快进合并,因此没有创建合并提交。
    • 我在 OP 中添加了 git 版本,我的版本根据你的回答解释了发生了什么。
    • @poke Danke - 我已经删除了误导性信息
    • @poke 那么,如何避免快进合并?
    • @octopusgrabbus Fast-foward merging 通常很有帮助,这绝对不是一件坏事,也不是您在问题中描述的原因。