如何在GIT推送时忽略远程更改?

3

我在尝试将一个项目推送到GIT时遇到了以下问题。

推送时,我收到了以下错误消息:

$ git push origin master
To https://bitbucket.org/MyAccount/my-project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://MyAccount@bitbucket.org/MyAccount/my-projec.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first 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.

看起来,如果我做出了错误的断言,请纠正我,我的远程存储库中有一些变更,我没有在本地存储库中获取到它们,并且建议我执行拉取以获取这些变更。

对我来说,这是一个问题,因为本地版本是我的应用程序的最后一个确定版本,我不能冒险覆盖它,从远程存储库中获取旧的或错误的内容(或由他人制作的内容)。

我可以指定推送本地内容而不考虑远程更改吗?或者我如何检查我最后一次本地提交和远程内容之间的差异?


可能是Force "git push" to overwrite remote files的重复问题。 - 1615903
1个回答

7

是的,正如您所理解的那样,这是因为远程的master分支有新版本(可能是同事推送的)。除非您使用强制推送,否则您不能忽略新版本,否则会导致新版本丢失。

您应该从远程拉取新版本到本地,并将未推送的提交在新版本的顶部进行变基,并在git pull期间保留冲突文件作为本地版本,如果存在冲突。您可以使用以下命令:

git pull origin master --rebase -X theirs

您可以使用跟踪分支来检查远程是否有新版本。通常,master分支会跟踪到origin/master(您可以通过git branch -vv命令进行检查)。然后,您可以使用git status命令,Git将比较本地主分支和远程分支。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接