最近本地一个flutter项目因为当时使用可视化创建的时候出了一些问题,但是起初没有注意,后来因为需要新增一个语音插件,需要修改原生android MainActivity.java,才发现这个目录根本不存在,于是重新创建一个项目将原来那个项目文件拷入,等修改好后再使用git 连接原来那个项目的github仓库进行代码推送,发现老是提示如下错误
$ git push -u origin master
To https://github.com/dengxiaoning/flutter-trip.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/dengxiaoning/flutter-trip.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
【注意】我们可以增加参数 --rebase进行拉取将本地与远程合并,但是拉取后本地代码需要手动修改冲突
如
git pull origin master --rebase
然后又使用pull 仓库代码拉取合并,但是推送任然失败,看有朋友说需要 增加 -f 进行强制拉取,但是试过也没用,最后反过来想来过强制推送看得行不
$ git push -f origin master
Counting objects: 173, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (130/130), done.
Writing objects: 100% (173/173), 1.66 MiB | 5.73 MiB/s, done.
Total 173 (delta 9), reused 38 (delta 4)
remote: Resolving deltas: 100% (9/9), done.
To https://github.com/dengxiaoning/flutter-trip.git
+ d137bd8...0208d12 master -> master (forced update)
一试咦还OK了,不错记录记录。
【注意】在协调时慎用,该参数-f
会使用本地项目完全替换掉原仓库的项目,如果本地项目不能保证比远程仓库的完整切勿使用,否则追悔莫及
看看官方介绍
-f--force
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. Also, when --force-with-lease option is used, the command refuses to update a remote ref whose current value does not match what is expected.
This flag disables these checks, and can cause the remote repository to lose commits; use it with care.
Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details.
【个人建议】我觉得最好的办法还是将自己本地修改的项目拷贝一份,然后新建项目目录重新拉取远程仓库代码,最后用本地修改的文件覆盖刚拉取的代码即可(前提还是在你修改期间,其他同事未有提交)