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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41283955/
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-09 04:42:34  来源:igfitidea点击:

GitHub keeps saying "This branch is X commits ahead, Y commits behind"

gitgithub

提问by Fernando Garcia

I know there are several similar questions, but I think my situation is a little bit different.

我知道有几个类似的问题,但我认为我的情况有点不同。

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.

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

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.

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

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 plusX more commits, that are the merges I did on my fork to synchronize with the original repository.

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

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.

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

Do you have any ideas?

你有什么想法?

回答by Scott Weldon

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:

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

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 --rebaseand --force-with-leaseoptions will only be necessary if you have commits that haven't been merged into the upstream repo.)

(只有当您的提交尚未合并到上游存储库中时,才需要--rebaseand--force-with-lease选项。)

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.

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



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-pickthem 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.

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

    git checkout master
    git reset --hard upstream/master
    
  3. 如果您在 fork 中创建了任何提交,则可以将cherry-pick它们添加到更新版本的master. 如果您不记得或需要帮助找到它们,例如

    git log --oneline master origin/master
    

    应该向您显示不在上游的任何提交。



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 --rebaseor --force-with-lease:

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

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

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

回答by Adam

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

1)将您的本地存储库“重置”到大量提交之前的那一刻

2) Create a new branch using this amended local repo

2)使用这个修改后的本地仓库创建一个新分支

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

3) 将此修改后的本地存储库“发布”到您的 github 存储库

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

4) 在修改后的本地 repo 中进行你想要 PR 到 github 的更改

5) 'Commit' this local repo

5)“提交”这个本地仓库

6) 'Pull' the commit to your github repo

6) 将提交“拉”到您的 github 存储库

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

7) 在你的 github repo 新分支上,向上游 repo 提交 pull request

Hope this answer could help.

希望这个答案能有所帮助。