GitHub一直在说“此分支是X提交在前,Y提交在后面". [英] GitHub keeps saying "This branch is X commits ahead, Y commits behind"
问题描述
我知道有几个类似的问题,但我认为我的情况有所不同.
I know there are several similar questions, but I think my situation is a little bit different.
假设有一个我想贡献的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,单击"New Pull"请求,确保我选择我的作为基础,并选择原始主数据库作为头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"是我执行上述同步过程的次数.因此,每次我向原始存储库(而不是我的fork)中执行一次拉取请求时,都表明我在提交我的代码 plus X次以上的提交,这就是我对fork进行的合并与原始存储库同步.
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.)
强制性的警告:由于重新建立基础会重写历史记录,这对于在该分支机构工作的任何其他人都是危险的/破坏性的.确保与与您合作的任何人清楚地交流您所做的事情.由于这是个人叉子,因此我认为这对您来说不是问题.
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.
- 如上所述添加上游遥控器.
-
重置您的本地分支以匹配
upstream
:
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屋!