不知何故,我的主分支和我的起源/主分支分道扬镳了。 我不希望它们发散。

我如何看待这些差异并将其合并?


当前回答

你可能会遇到这种情况,只需要从remote中获取1个历史记录:

$ git pull --depth=1
fatal: refusing to merge unrelated histories
$ git status
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.

根据上面的回答,会导致两个分支分流到不同的“线”,所以Git认为这是不相关的历史。

---a---b---main
        \      \
          x x x x   diverged, cannot be merged anymore
          \      \
    ---?---?---?---c(origin/main)

最后,简单的解决方案是:git重置-硬的origin/main,如果你不关心本地的变化,否则你将失去所有的工作。

或者尝试git pull——depth=2。

其他回答

要查看差异:

我嫉妒他

这将显示两个分支之间的变化或差异。在araxis(我最喜欢的)中,它以文件夹差异样式显示它。显示每个更改的文件。然后,我可以单击一个文件来查看文件中更改的详细信息。

你可能会遇到这种情况,只需要从remote中获取1个历史记录:

$ git pull --depth=1
fatal: refusing to merge unrelated histories
$ git status
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.

根据上面的回答,会导致两个分支分流到不同的“线”,所以Git认为这是不相关的历史。

---a---b---main
        \      \
          x x x x   diverged, cannot be merged anymore
          \      \
    ---?---?---?---c(origin/main)

最后,简单的解决方案是:git重置-硬的origin/main,如果你不关心本地的变化,否则你将失去所有的工作。

或者尝试git pull——depth=2。

我有这个问题,即使在阅读了上面的回复后,我也不知道是什么导致了它。我的解决办法是做

git reset --hard origin/master

然后,这只是将我的(本地)master副本(我认为是搞砸了)重置到正确的点,由(远程)origin/master表示。

警告:您将丢失所有尚未推送到原点/主节点的更改。

在我的例子中,这里是我所做的导致分歧的消息:我做了git push,但后来做了git commit——amend,以向commit消息中添加一些东西。然后我又做了另一次提交。

所以在我的例子中,这仅仅意味着原点/主节点已经过时了。因为我知道没有其他人在触摸原点/master,修复是微不足道的:git push -f(其中-f表示力)

我更喜欢更方便、更安全的方式。

# copying your commit(s) to separate branch
git checkout <last_sync_commit>
git checkout -b temp
git cherry-pick <last_local_commit>

git checkout master
git reset --soft HEAD~1 # or how many commits you have only on local machine
git stash               # safer, can be avoided using hard resetting on the above line
git pull
git cherry-pick <last_local_commit>

# deleting temporary branch
git branch -D temp