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

How to ignore the remote changes during a GIT push?

我在尝试在 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.

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

这对我来说是个问题,因为本地版本是我的应用程序的最后一个最终版本,我不能冒险覆盖远程存储库中旧的和错误的(或由其他人制作的)东西。

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

是的,如您所知,这是因为远程 master 分支有新版本(可能是您的同事推送的)。而且不能忽略新版本,除非强行push,否则会导致新版本丢失。

您应该将新版本从远程拉取到本地,并将未推送的提交变基到新版本的顶部,如果在 git 拉取过程中发生冲突,请将冲突文件保留为本地版本,您的命令可以使用

git pull origin master --rebase -X theirs

您可以使用跟踪分支来检查远程是否有新版本。 master 分支通常跟踪到 origin/master(您可以通过 git branch –vv 查看)。然后你可以使用git status,git将在本地master和远程分支之间进行比较。