0

My branch in the Github repository is "master". In the terminal I did:

git branch

and the output is:

* main

my-temporary-work

I wanted to push a file to Github and I used:

git add exploratory_analysis.ipynb

git commit -m "New version"

git push origin master

The "git add" and "git commit" commands work. However, the last command (i.e., "git push origin master") does not work, and I get this error:

error: src refspec master does not match any

error: failed to push some refs to 'git@github.com:NAME-OF-USER/REPOSITORY-NAME.git'

That is, it looks like I cannot push any code from my laptop to Github. How can I fix it?

Franz
  • 45
  • 1
  • 7
  • You literally cannot push *files* to another Git repository. You can only push *commits*. Commits *contain* files, but you either get the whole commit sent, or nothing sent. Your [conversation with Davis S](https://stackoverflow.com/a/65327147/1256452) is on the right track, but you'll need to make sure you have all the commits that the upstream (GitHub) repository has, before you try to add a commit on to their commits. – torek Dec 17 '20 at 06:02

2 Answers2

3

I faced the same issue some days ago. If you created a new repository nowadays(2020) then the default branch in main on GitHub.

you can check on GitHub now in your repository branches.

so that's why you need to run

git push origin main

instead of

git push origin master

Goodluck with more details you can watch video

Arslan Ahmad khan
  • 5,426
  • 1
  • 27
  • 33
  • 1
    Thank you. I deleted a few files from my laptop which were not pushed yet, and then "git push origin main" worked on a single file. – Franz Dec 17 '20 at 09:11
  • I am having a similar problem but mine is main branch and i tried what he did by saying to push to master but I can't get it resolved – AlThePal78 Oct 02 '21 at 20:53
  • your suggestion did not work for me, instead i used git push origin main -f – afikri Oct 18 '21 at 00:59
0

Why are you pushing to master if the branch commands outputs main? Try 'git push origin main'

Also found this explanation for the change: https://stackoverflow.com/a/65008828/1959534

David S
  • 195
  • 5
  • 19
  • Hello, I also tried to use 'git push origin main'. However, if I use 'git push origin main', then all files in the folder are pushed from my laptop into Github. When I did 'git push origin main', then approximately 4000 files were pushed into Github, and it took quite a long time. My goal is to push one single file at a time (e.g., push only exploratory_analysis.ipynb). – Franz Dec 16 '20 at 16:28
  • You sure there isn't anything else that is staged (check with git status)? If you only did git add on exploratory_analysis.ipynb and committed it then that is that will be pushed. – David S Dec 16 '20 at 16:34
  • Hi David S, I just did "git status" now. This is the result of "git status": **On branch main** **Your branch is ahead of 'origin/main' by 19 commits.** **(use "git push" to publish your local commits)** **nothing to commit, working tree clean** – Franz Dec 16 '20 at 16:38
  • Akey, that explains why there are more files that are pushed, there are an additional 18 commits that are pending locally. Should they be pushed also? If not you can unstage all of them with 'git reset HEAD~19' and then re-stage your one file that you want to push. – David S Dec 16 '20 at 16:43
  • I did that but it does not work. I did 'git reset HEAD~19' but then I still get the same error. – Franz Dec 16 '20 at 17:35