如何重新创建“git error fatal:Need to specify how to reconciliate divergent branches."?

moiiocjp  于 2024-01-04  发布在  Git
关注(0)|答案(1)|浏览(1158)

这个问题不是问如何修复这个错误,但如何在本地测试存储库中重新创建它,所以我可以真正理解这个错误的实际含义.我找不到一个很好的解释在这里或HEREHEREHERE .一种方法来重新创建这个错误被描述HERE,但这个描述是不完整的.
我想学习的原因是,当我试图从远程仓库(存储在其他计算机上)更新本地分支(我本地计算机上的分支)时,我遇到了git的问题。

git pull
git pull --ff-only 
git reset --hard feature/branch
etc.

字符串
也看到了this question,这对我没有帮助。
似乎是一个同事强制推到了那个分支,所以我不得不重置我的本地分支?(不确定这实际上意味着什么)。我试着“重置”我的本地分支,查看上面的所有命令和this question。但是它不起作用。
最后的解决办法是做(当在该分支上时)

git reset --hard 6c3b91e   # <- a commit not in that branch ?????
git pull --ff-only


我问同事这是怎么回事,但他无法解释为什么我必须重置到一个甚至不在分支上的提交。
因此,我想自己重现这种情况,以了解发生了什么,了解正在发生的事情(就分支,提交,索引,HEAD等而言),以更好地理解和学习Git是如何工作的。
请不要建议书籍或文章,他们从来没有解释任何错误,在一个简洁明了的方式,我可能会理解.
有趣的是,我在main分支上做了修改,但没有将其推送到远程,然后又做了一个git pull,从而重现了这个错误。但首先,昨天发生这个错误时,我不在主分支上,其次,我对分支上的任何修改都不感兴趣。我想让分支在本地与远程仓库上的分支相等。

6kkfgxo0

6kkfgxo01#

创建“diverged”分支来显示你的问题真的很简单。错误消息取决于你的特定Git版本,也可能取决于本地配置。

$ git init
Initialized empty Git repository in ...
$ git config --unset pull.rebase
$ git config --unset pull.ff
$ cat > todos <<EOF
> my list
> EOF
$ git add todos
$ git commit -m 'initial todos'
[master (root-commit) e729989] initial todos
 1 file changed, 1 insertion(+)
 create mode 100644 todos
$ git checkout -b original
Switched to a new branch 'original'
$ cat >> todos <<EOF
> 
> milk
> EOF
$ git add todos
$ git commit -m 'buy milk'
[original 8b36329] buy milk
 1 file changed, 2 insertions(+)
$ git checkout -b other
Switched to a new branch 'other'
$ sed -i 's/^milk$/eggs/' todos
$ git add todos
$ git commit --amend -m 'buy eggs' # this diverges the branches. there are other possibilities, including rebase and reset
[other a318636] buy eggs
 Date: Thu Dec 21 12:00:35 2023 +0100
 1 file changed, 2 insertions(+)
$ git checkout original
Switched to branch 'original'
$ git pull . other
From .
 * branch            other      -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
$ git pull --ff-only . other
From .
 * branch            other      -> FETCH_HEAD
hint: Diverging branches can't be fast-forwarded, you need to either:
hint: 
hint:   git merge --no-ff
hint: 
hint: or:
hint: 
hint:   git rebase
hint: 
hint: Disable this message with "git config advice.diverging false"
fatal: Not possible to fast-forward, aborting.
$

字符串

相关问题