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

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


当前回答

你可能会遇到这种情况,只需要从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重置——soft origin/my_remote_tracking_branch

This way you will not loose your local changes

我已经通过移动到commit_sha来修复它,最后提交给origin/master。

git reset --hard commit_sha

警告:在commit 'commit_sha' commit之后,你将丢失所有提交的内容。

当我基于分支a创建一个分支时遇到了这个问题

git checkout -b a

然后我将分支a的起始流设置为分支B的原点

git branch -u origin/B

然后我得到了上面的错误消息。

对我来说,解决这个问题的一个方法是,

删除分支a 创建一个新的分支b

git checkout -b b origin/B

在我的例子中,这里是我所做的导致分歧的消息:我做了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