1. Error description
Appears when uploading after creating a warehouse git
on ! [rejected] main -> main (fetch first)
, error: failed to push some refs to *****
.
2. The cause of the error
When we create a warehouse, we will check the Add README
file . This operation automatically creates a README
file and configures to add the ignore file. When you click to create a warehouse, the system will automatically make an initial submission for us. At this time, the empty warehouse will have a README.md
readme . At this point, we will associate the remote library with the local. Both ends now have content, and there is no connection between the two contents. When we push the local content to the remote end or pull the content from the remote end, there is no connection. The tracked history causes the push or pull to fail. At this time, the above error git
will be reported.
3. Solutions
- <1> First, backup the local code to another directory (
git
not the directory); - <2> Delete the files in the local directory to be pushed, except
.git
the directory ; - <3> Use the command
git pull --rebase origin main
to synchronize the remote content to the local; - <4> Copy the backup code back to the current directory to be pushed;
- <5> Regularly push your code,
git add .
; - <6>
git commit -m "Init Commit"
; - <7>
git push -u origin main
;
At this time, you will find that the code will be pushed to the remote library normally.
[注]:第一步一定要备份代码,否则可能会在拉取的时候删除你辛辛苦苦写好的代码。