本地代码与远程存储库不同步

时间:2019-12-26 06:26:09

标签: git bitbucket

我们面临以下问题,其中我们的本地代码与远程存储库不同步。

我们最近将代码(未在Git早期版本中维护)推送到Git存储库中。将本地副本推送到Git存储库后,我们意识到,我们在本地计算机的另一条路径中有一些更新的本地副本,不要试图将这些东西推送到远程存储库。在推送更改时,我们将遇到以下问题。

$ git push origin master
To https://vcs.jd-staging.com/bitbucket/scm/bmsg/bmsg.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://vcs.jd-staging.com/bitbucket/scm/bmsg/bmsg.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


$ git branch -r
  origin/master

$ git pull origin master
From https://vcs.jd-staging.com/bitbucket/scm/bmsg/bmsg
 * branch            master     -> FETCH_HEAD
Already up to date.

$ git push origin master
To https://vcs.jd-staging.com/bitbucket/scm/bmsg/bmsg.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://vcs.jd-staging.com/bitbucket/scm/bmsg/bmsg.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull --rebase origin master
From https://vcs.jd-staging.com/bitbucket/scm/bmsg/bmsg
 * branch            master     -> FETCH_HEAD
Already up to date.
fatal: It seems that there is already a rebase-apply directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
        git rebase (--continue | --abort | --skip)
If that is not the case, please
        rm -fr ".git/rebase-apply"
and run me again.  I am stopping in case you still have something valuable there.

$ git pull origin master
From https://vcs.jd-staging.com/bitbucket/scm/bmsg/bmsg
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

1 个答案:

答案 0 :(得分:2)

最简单的git pull可能会解决此问题:

# from master
git pull origin master

但是请注意,这可能会导致本地合并冲突,您必须解决(紧跟着git commit)。这里的另一种选择是使用变基策略进行拉动:

git pull --rebase origin master

这将保留您的本地提交,尽管它仍然可能导致合并冲突的发生。

编辑:

错误消息refusing to merge unrelated histories表示您的本地存储库与远程存储库不同。也就是说,您的本地存储库包含一些项目,而不是您指向的远程项目。发生这种情况的原因可能是远程历史记录已更改,或者您在本地配置错误。