【问题标题】:Local branch ahead of master after git pullgit pull 之后在 master 之前的本地分支
【发布时间】:2020-11-05 19:24:26
【问题描述】:

我正在一个没有我的 git 凭据的远程服务器上工作,我想从 master 中提取。我没有本地提交,我只想更新这台机器上的本地存储库以匹配远程存储库(来源)。使用this answer 我设法更新了存储库:

my_repo$ git pull https://my_user@github.com/my_repo
Password for 'https://my_user@github.com': 
<some updates....>

确实,在此操作之后,本地存储库已更新为包含所有提交完全,就像在远程存储库上一样。

但是,由于某种原因,我无法理解,本地存储库变得领先 master,我找不到修复它的方法。这怎么可能???

my_repo$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
    

my_repo$ git push https://my_user@github.com/my_repo
Password for 'https://my_user@github.com': 
Everything up-to-date

my_repo$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

my_repo$ git pull https://my_user@github.com/my_repo
Password for 'https://my_user@github.com': 
From https://github.com/my_repo
 * branch            HEAD       -> FETCH_HEAD
Already up to date.

另外,当我使用git log 时,我看到了这个奇怪的结果,这对应于我的分支领先于master,但不对应于“现实”:

my_repo$ git log --pretty=oneline
09ee1f2 (HEAD -> master) Latest commit on master
b6433fb Another commit from master
31da031 Another commit from master
85b95ae (origin/master, origin/HEAD) Another commit from master which was the head before pulled

有人对如何解决这个问题有任何建议吗?有什么想法是如何发生的以及我将来如何避免这种情况?

谢谢!

【问题讨论】:

  • 你是在推到你要拉的同一个地方吗? git remote -vv 的输出是什么?
  • 是的@WilliamPursell,它是同一个仓库。 git remote -vv 给出的输出是origin https://github.com/my_repo.git (fetch) \n origin https://github.com/my_repo.git (push),和我的个人电脑上的输出完全一样
  • 让我们来看看小事:当你开始工作时,你的 repo 已经在 85b95ae 上,你创建了三个本地提交,你拉了(没有什么可以拉的),你的主人领先。会是这样吗?我会说是的,因为“已经是最新的”。在拉取消息中。
  • @DaemonPainter 实际上我没有进行本地提交。这就是奥秘。现在“领先于 master”的 3 个新提交是我从 原始 master 中提取的提交。我在我的个人电脑上进行了这些提交......:|
  • 也许edit() 你的问题要更加强调这一点。有多少台机器参与其中?我现在要说三个:远程、本地和您的个人。此外,您说 85b95ae 是拉动前的头部,进一步确认您的本地人在那里。但是,以防万一,进入远程并检查哪个是 HEAD 提交。请打印 SHA

标签: git


【解决方案1】:

您的来源/主人已过时。

从 URL 拉取是一次性的。它不会更新 origin/master。

origin 是与特定 URL 关联的远程存储库的名称。 origin/master 是您最后一次在 origin 上看到 master 分支。 origin/master 将仅使用来自 origin 的 git fetch 或 git pull 进行更新。这还没有发生。

相反,您通过从一些不同的 URL 拉取更新了 master。它可能与原始存储库相同,但 Git 不知道。您可以通过使用git remote set-url origin https://my_user@github.com/my_repo 更改原始网址来解决此问题。然后git fetch origin。

在 Pro Git 中查看 Remote Branches。

【讨论】:

  • 感谢您的回答!问题是,这是一台远程机器,我不是唯一一个在使用它的人,如果我更改远程地址,那么下一个人也必须更改它,对吗? (有没有办法在同一台计算机上协同工作?)
  • @DalyaG 我不确定你的意思。您是否与其他用户共享帐户或克隆的存储库?
  • @DalyaG 如果您正在 ssh 到远程计算机并使用那里的存储库并且您不想复制您的 Github ssh 密钥,您可以使用 SSH Agent Forwarding 在远程使用您的本地 ssh 凭据机器。
  • 这是一台我和另一位同事都在开发的远程机器,我们都希望能够使用我们自己的单独凭据进行拉取和推送操作。
【解决方案2】:

感谢@Schwern's answer,我才能够理解这个问题。

我是如何解决的:

  1. 使用this 精彩网站恢复“本地”提交:

     git reset --hard @{u}
    
  2. 确保removing credentials 来自gitconfig 的此计算机上的此存储库上没有现有凭据:

     sudo subl ~/.gitconfig  // or any other text editing instead of subl
    
  3. “定期”拉取,即不指定我的凭据:

     my_repo$ git pull
     Username for 'https//github.com': my_user
     Password for 'https://my_user@github.com':
     <updates...>
    

现在一切都很好......

再次感谢@Schwern 解释问题!

【讨论】:

    猜你喜欢
    • 2015-10-06
    • 2012-07-26
    • 1970-01-01
    • 2022-11-10
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2013-12-04
    相关资源
    最近更新 更多