I have created a git repository locally, as shown below
doudo@LAPTOP-P5H3MDKN MINGW64 /d/software/java/java_work/DataStruct/DataStructureAndAlgorithm (master)
$ git push origin master
error: src refspec master does not match any
error: failed to push some refs to 'github.com:douzhenjun/DataStruct.git'
doudo@LAPTOP-P5H3MDKN MINGW64 /d/software/java/java_work/DataStruct/DataStructureAndAlgorithm (master)
$ git branch
doudo@LAPTOP-P5H3MDKN MINGW64 /d/software/java/java_work/DataStruct/DataStructureAndAlgorithm (master)
$ git push --set-upstream origin master
error: src refspec master does not match any
error: failed to push some refs to 'github.com:douzhenjun/DataStruct.git'
I have added remote orgin repository on github,
but when I attempt to push cotents to remote origin, fatals above occurs. Moreover, I have built a new branch named dou and ever switched to it, but I can't switch again to master. This branch(named master) is what I rebuilt, when I key in command git branch, there is nothing to see. Why is it ?
doudo@LAPTOP-P5H3MDKN MINGW64 /d/software/java/java_work/DataStruct/DataStructureAndAlgorithm (master)
$ ll -a
total 29
drwxr-xr-x 1 doudo 197121 0 Apr 20 13:52 ./
drwxr-xr-x 1 doudo 197121 0 Apr 16 2021 ../
drwxr-xr-x 1 doudo 197121 0 Apr 20 20:26 .git/
drwxr-xr-x 1 doudo 197121 0 Apr 4 14:21 .idea/
-rw-r--r-- 1 doudo 197121 455 Jun 22 2020 DataStructureAndAlgorithm.iml
drwxr-xr-x 1 doudo 197121 0 Aug 30 2020 out/
drwxr-xr-x 1 doudo 197121 0 Apr 19 21:05 src/
my remote repository on github
CodePudding user response:
Git is about commits, not branches.
"but I can't switch again to master" Because it doesn't exist. You never did an add and commit on master
so it was an "unborn" branch.
Therefore you can't push it (you never made any commits to push) and your git branch
output is empty (you have no branches); and having switched to a different branch you can't switch back (there is nothing to go back to).
If you truly have a dou
branch with commits, you could now rename it master
and push it. But you have not proved that you do.
But it would be better at this point to forget master
because your main branch at GitHub is called main
. You might want to read my article https://www.biteinteractive.com/of-git-and-github-master-and-main/.
CodePudding user response:
First make sure your SSH key has been configured correctly, then certify that you have your remote repo configured with
echo "remote name: $(git remote)" "URL: $(git remote get-url $(git remote))"
this must show your repo "nickname" and repo-URL like this:
remote name: origin
URL: [email protected]:douzhenjun/repo_name.git
Then with the command git push --set-upstream origin master
you configure the branch master in your remote repo as the "target" for push. And with git push -u origin master
you can speciy what remote repo and branch are you pushing into.
Note that origin
is the name configured when you run the command git remote add {name} {URL}
, the command git remote
print this name.
Remember to commit all local changes before you push to remote content, an basic "workflow" of git is pull -> commits -> push
Before edit run git pull
, this will update your local repo with remote content, then after you finish your modifications you will commit your modifications, then push to remote content.