An error analysis for Git file upload

Requirements: upload local files via git to github remote repository

The source code is as follows

Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop
$ mkdir test

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop
$ cd test

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop/test
$ git init
Initialized empty Git repository in C:/Users/Zhao Yafeng/Desktop/test/.git/

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop/test (master)
$ git add .

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop/test (master)
$ git commit -m 'An incorrect submission'
[master (root-commit) 096e483] An incorrect submission
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop/test (master)
$ git remote add origin git@github.com:AFeng521web/test.git

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop/test (master)
$ git push -u origin master
To github.com:AFeng521web/test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:AFeng521web/test.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.

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop/test (master)
$ git pull --rebase origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:AFeng521web/test
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: An incorrect submission

//Zhao Yafeng@DESKTOP-0JOAFKG MINGW64 ~/Desktop/test (master)
$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:AFeng521web/test.git
   0ecc51a..8696bf9  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

To github.com:AFeng521web/test.git

! [rejected] master -> master (fetch first)

error: failed to push some refs to 'git@github.com:AFeng521web/test.git'

Next, analyze the reasons for this error:

Since the readme.md file was created on a remote github, the upload has been unsuccessful.

Solution

$ git pull --rebase origin master

Pull the remote readme.md file locally and use git push-u origin master
Associate the remote library, and the upload will succeed.

One caveat: If you upload local files through git, it's best not to create readme.md files when the github repository is created.

Process for uploading a local file

  1. Under the folder you need to upload, open Git bash here.
  2. git init initializes this folder and generates the.git folder.
  3. git add. (Note that this has a point) Upload all files.
  4. Git commit-m'Description information', submitted to local repository.
  5. git remote add origin git@github.com:AFeng521web/repository name.git, associated remote library.
  6. Git push-u origin master, submitted to remote repository.

Tags: git github

Posted on Sat, 07 Mar 2020 12:39:20 -0500 by getgray