GitHub 一直在说“这个分支是 X 提前提交,Y 提交在后面”

假设有一个我想贡献的 GitHub 存储库。 我将这个存储库分叉到我的 GitHub 帐户中,然后在我的电脑中从我的帐户中克隆这个分叉。

在处理某个问题之前,我首先要将 fork 与“原始”存储库同步。我走到我的帐户分叉,点击 New Pull 请求,确保我选择我的作为基础和原始的主作为头分叉,我看到了差异(所有的提交,人们在原来的存储库,不是在我的)。 然后在 fork 上创建 pull 请求,并在 fork 中合并这些更改。 我去当地的回收站做一个 git pull-一切都同步了-好吧。

现在问题来了,在我的 GitHub 帐户中,它总是说‘这个分支是提前提交的 X’,其中‘ X’是我执行上面描述的同步进程的次数。因此,每次我向原始存储库(而不是我的 fork)执行一个 pull 请求时,都会显示我正在提交代码 还有 X 更多的提交,这是我在 fork 上执行的与原始存储库同步的合并操作。

当然,我不想将这些更改推入原始存储库,因为它们已经有了这些更改,所以我不明白为什么 GitHub 一直告诉我要提交更改。

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

你有什么主意吗?

91265 次浏览

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

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

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

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. Add the upstream remote as described above.
  2. Reset your local branch to match upstream:

    git checkout master
    git reset --hard upstream/master
    
  3. 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.


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

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

I have the same problem with you and just solved this problem.

To solve this:

  1. 'Reset' your local repo to the moment before the abundant commits

  2. Create a new branch using this amended local repo

  3. 'Publish' this amended local repo to your github repo

  4. Make the changes that you want to PR to github in the amended local repo

  5. 'Commit' this local repo

  6. 'Pull' the commit to your github repo

  7. On your github repo new branch, submit pull request to the upstream repo

Hope this answer could help.

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 [...]

If you want to update/syncronize github forks, you should not use a Pull Request.

Pull Requests introduce merge commits, which are the source of your error message. (Pull Requests are not fast-forward by default). The merge commits exist in your fork, but not in the source repo.

You don't want to merge their branch with your branch... you want to update your branch to point to the same commit as their branch. You want the branches between forks to be the same.

You can do this in various ways, best explained in other answers: