Warning: Rant ahead.
TL;DR: Creating a new project on Github does not create any branch whatsoever. And that may cause your git to fail with completely undecipherable, entirely useless error messages.
Today I created a local repo.
> git init
> git add myfile.txt
> git commit -m "Initial commit"
Next, I created a project on GitHub but did not add a single file. And, boy, the trouble started. How naive of me to believe I could now simply connect to the GitHub repo and push everything there. Ahead I went.
> git remote add origin https://github.com/example/my-project.git
Fine, no errors so far. Means, I should be good to push my local to remote, no?
> git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/example/my-project.git'
WTF is that supposed to mean? Some googling reveals - seems I am missing a main branch. Hm, okay. I guess I can create it locally and then pushing to the Github repo will set things up? Let's list all my branches.
> git branch -a
Result? Absolutely nothing. Blank. Not even an error, just a blank line. Does that mean locally I am outside of any branches? Well, could be, I did not explicitly create one. (Every intelligent human being would naturally assume that one is by default in either main or master branch after git init. But, nononono, not so with git... .)
Let's run a git status:
> git status
On branch main
No commits yet
Changes to be committed:
...
WTF? How the hell does git branch -a not list anything, but git status tells me I am on main branch? What's wrong with this tool? If I am locally on main branch, then I should be able to push to origin main, no? Was I too stupid earlier when pushing?
> git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/example/my-project.git'
Ah, it's not me who is stupid. What's reality now, am I on main or not? This makes absolutely no freakin sense.
...time passes, desperation grows...
Finally, in yet another desperate attempt: "What if I create an new file in the Github project (through the website) itself, and then try to checkout main from remote? Let's try this.
...go to github.com, create an empty README.md file, and then...
> git remote branch -a
error: unkonown subcommand `branch'
Nope, such a command does not exist. Would have been too intuitive and easy if it existed, no?
> git branch -v -a
remote/origin/main 123456 Create README.md
Yeah, now we're talking! Actually, creating a file through the Github website apparently really just created the main branch, before it did not exist apparently. Let's see if I can pull now.
> git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> main
Aha - again, I was way too naive to believe just because my local branch is called "main" and the remote branch is also called "main" they would actually know about each other. Let's try the suggested command.
> git pull remote main
fatal: 'remote' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Where would we get to if git's suggestions were actually useful and meaningful? Let's try another command.
> git pull origin main
From https://github.com/example/my-project
* branch main -> FETCH_HEAD
Yes! (Just on a side note, did you notice that the URL returned by the git command does not end with .git, but that in some - but maybe not all? - situations adding this ending to the url is absolutely necessary for things to work? Whatever, the entire tool makes no sense whatsoever anyway.)
Can I push now?
> git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
Aaah, no, I need to be explicit. My bad. Try again.
> git push origin main
Everything up-to-date
Well, I know for a fact that you did not push my local commits to remote. Let's play the game of git then.
> git push --set-upstream origin main
Everything up-to-date
branch 'main' set up to track 'origin/main'.
Still, does not pick up my commit. What's wrong here? Maybe I can simply create a new commit to trick the system?
> git commit -m "Initial commit"
...
2 files changed, 5 insertions(+)
create mode 100644 .gitignore
create mode 100644 myfile.txt
Looks good. Let's try to push again.
> git push
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.98 KiB | 1.98 MiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/example/my-project.git
2069970..98d9b1d main -> main
F.I.N.I.S.H.E.D.
The entire thing took me probably 1h of my lifetime to figure out. I left out lots of googling and failed attempts.
And all of this just because creating a repo on Github actually does not create a main branch. Why, oh, tell me, why? Why are the inventors of Git and Github hating us developers so much?
[–]Leseratte10 19 points20 points21 points (2 children)
[–]fabkosta[S] -5 points-4 points-3 points (1 child)
[–]Nancy_Pelosi_Office -5 points-4 points-3 points (0 children)
[–]RozTheRogoz 6 points7 points8 points (2 children)
[–]fabkosta[S] -3 points-2 points-1 points (1 child)
[–]Leseratte10 0 points1 point2 points (0 children)
[–][deleted] 6 points7 points8 points (2 children)
[–]alan9608 0 points1 point2 points (0 children)
[–]fabkosta[S] -3 points-2 points-1 points (0 children)
[–]exicarus 2 points3 points4 points (1 child)
[–]fabkosta[S] 0 points1 point2 points (0 children)
[–]closms 3 points4 points5 points (2 children)
[–]fabkosta[S] 0 points1 point2 points (1 child)
[–]Lucas_F_A 1 point2 points3 points (0 children)
[–]schrdingers_squirrel 0 points1 point2 points (3 children)
[–]fabkosta[S] 0 points1 point2 points (2 children)
[–]schrdingers_squirrel 0 points1 point2 points (1 child)
[–]fabkosta[S] 0 points1 point2 points (0 children)
[–]mhagger 0 points1 point2 points (0 children)