GitHub 一直说“这个分支 X 提交在前面,Y 提交在后面"; [英] GitHub keeps saying "This branch is X commits ahead, Y commits behind"

查看:30
本文介绍了GitHub 一直说“这个分支 X 提交在前面,Y 提交在后面";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个我想贡献的 GitHub 存储库.我将该存储库分叉到我的 GitHub 帐户中,然后从我的 PC 帐户中克隆该分叉.很好.

Let's say there is a GitHub repository which I want to contribute to. I fork that repository into my GitHub account and I clone the fork from my account in my PC. Fine.

在处理问题之前,我首先想将我的 fork 与原始"存储库同步.我转到我的帐户分叉,单击新拉取请求,确保我选择我的作为基础和原始主作为头分叉,我看到了差异(人们在原始存储库中所做的所有提交都不是我的).然后我在我的叉子上创建拉取请求,并将这些更改合并到我的叉子中.我去我的本地存储库并执行 git pull,并且我已经同步了所有内容.很好.

Before working on an issue, I first want to synchronize my fork with the 'original' repository. I go to my account fork, click on New Pull request, make sure that I select mine as base and the original master as head fork, I see the differences (all the commits that people did in the original repository that are not on mine). Then I create the pull request on my fork and I merge those changes in my fork. I go to my local repo and do a git pull, and I have everything synchronized. Fine.

现在问题来了,在我的 GitHub 帐户中,现在总是说此分支是 X 次提交",其中X"是我执行上述同步过程的次数.因此,每次我向原始存储库(不是我的叉子)发出拉取请求时,都表明我正在提交我的代码 加上 X 次更多提交,这是我在叉子上所做的合并与原始存储库同步.

The problem comes now, in my GitHub account now it's always saying 'This branch is X commits ahead', where 'X' is the number of times that I did the synchronize process I described above. So, every time I do a pull request into the original repository (not my fork), is showing that I'm committing my code plus X more commits, that are the merges I did on my fork to synchronize with the original repository.

当然,我不想将这些更改推送到原始存储库中,因为它们已经有了这些更改,所以我不明白为什么 GitHub 一直对我说我有更改要提交.

Of course, I don't want to push those changes into the original repository, since they already have those changes in place, so I don't understand why GitHub keeps saying to me that I have changes to commit.

我认为这是必须在我的 GitHub 帐户上解决的问题,因为在我的本地存储库中没有任何更改或问题,实际上我什至将其删除并再次重新克隆.

I think it's something that has to be solved on my GitHub account, because in my local repository there are no changes or issues, actually I even removed it and re-cloned again.

你有什么想法吗?

推荐答案

如您所料,这些额外的提交很可能是您创建的合并请求的合并提交.

As you guessed, these extra commits are likely the merge commits from the Pull Requests that you created.

在未来,有一种更简单的方法可以将您的 fork 与原始存储库同步.在您的本地存储库中,在初始克隆之后执行:

In the future, there's a much easier way to sync your fork with the original repository. In your local repo, after the initial clone do:

git remote add upstream https://github.com/upstream/repo.git

然后,无论何时您想从上游同步更改,请执行以下操作:

Then, whenever you want to sync the changes from upstream, do:

git pull --rebase upstream master
git push --force-with-lease origin master

(--rebase--force-with-lease 选项仅在您有尚未合并到上游存储库的提交时才需要.)

(The --rebase and --force-with-lease options will only be necessary if you have commits that haven't been merged into the upstream repo.)

强制性警告:由于 rebase 会重写历史记录,因此对于在此分支上工作的任何其他人来说,这可能是危险的/具有破坏性的.确保您与正在合作的任何人清楚地传达您所做的事情.由于这是个人分叉,我认为这对您来说不是问题.

Obligatory warning: Since a rebase rewrites history, this can be dangerous / disruptive for anyone else working on this branch. Be sure you clearly communicate what you have done with anyone you are collaborating with. Since this is a personal fork, I assume this won't be an issue for you.

现在在事后解决您当前的问题.

Now to fix your current issue after the fact.

  1. 如上所述添加上游遥控器.
  2. 重置您的本地分支以匹配上游:

git checkout master
git reset --hard upstream/master

  • 如果您在 fork 中创建了任何提交,您可以将它们cherry-pick 到您的 master 的更新版本中.如果您不记得或需要帮助找到它们,例如

  • If you have created any commits in your fork, you can cherry-pick them onto your updated version of master. If you can't remember or need help finding them, something like

    git log --oneline master origin/master
    

    应该显示任何不在上游的提交.

    should show you any commits not in upstream.

    <小时>

    上面我假设你只使用一个分支,master.如果您还没有,我强烈建议您为您处理的每个功能/错误修复创建一个新分支.除其他好处外,这允许您在仍在等待合并早期 PR 时开始处理另一个功能/错误修复.如果您从不直接提交给 master,那么您可以在没有 --rebase--force-with-lease 的情况下进行同步:


    Above I assumed that you are only using one branch, master. If you aren't already, I highly recommend that you create a new branch for each feature / bug fix that you work on. Among other benefits, this allows you to start work on another feature / bug fix when you are still waiting for an earlier PR to be merged. If you never commit directly to master, then you can sync without the --rebase or --force-with-lease:

    git checkout master
    git pull upstream master
    git push origin master
    

    要在更新 master 后更新功能分支,请执行以下操作:

    To update a feature branch after you have updated master, do:

    git checkout myfeature
    git rebase master
    git push --force-with-lease origin myfeature # if you have already pushed
    

    这篇关于GitHub 一直说“这个分支 X 提交在前面,Y 提交在后面";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆