问题:在修改完文件向github提交的时候,提交失败;由于之前直接在github上修改文件提交了一次,但是没有修改对,又再本地修改再次提交,就会失败,报如下错误:
hfzhang@010YS-IT-00368 MINGW64 /d/JAVA-01/JAVA-01/Week_07 (main)
$ git push origin
To github.com:areyoucreazy/JAVA-01.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'git@github.com:areyoucreazy/JAVA-01.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.
原因其实就是文件有冲突,没有解决的情况下直接提交造成的。
解决办法:
方案一:提交之前先执行 git pull 命令拉取代码,然后再提交,成功!
hfzhang@010YS-IT-00368 MINGW64 /d/JAVA-01/JAVA-01/Week_07 (main)
$ git pull
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 4), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
From github.com:areyoucreazy/JAVA-01
440448b..d51ef82 main -> origin/main
Auto-merging Week_07/README.md
Merge made by the 'recursive' strategy.
hfzhang@010YS-IT-00368 MINGW64 /d/JAVA-01/JAVA-01/Week_07 (main)
$ git push origin
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 591 bytes | 59.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
To github.com:areyoucreazy/JAVA-01.git
d51ef82..16e6954 main -> main
方案二:这是从网上找的,使用的命令是 git pull origin master --allow-unrelated-histories,然后提交的时候用的命令是 git push -u origin master
我查了一下这个命令 --allow-unrelated-histories ,其实是提醒有两个不相干的分支要进行合并,并且强制执行的意思;如果方案一不成功,可以试试这个