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

And*_*ili 3 git version-control bitbucket

我在尝试在 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.
Run Code Online (Sandbox Code Playgroud)

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

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

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

Mar*_*Liu 6

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

您应该将新版本从远程拉到本地,并将未推送的提交重新设置在新版本的顶部,如果在 git pull 期间发生冲突,则将冲突文件保留为本地版本,您可以使用该命令

git pull origin master --rebase -X theirs
Run Code Online (Sandbox Code Playgroud)

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