This is driving me up the wall!
This is what I've done:
- Created a folder with some code (local repo)
- Created a repo in Github e.g.
my_repo
- Issued these commands (and trial and error'ed hella other stuff too):
git init && \
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main && \
git remote add origin https://github.com/my_github/my_repo.git && \
git checkout -b init_branch && \
git add * && \
git commit -m "Initial commit" && \
git push origin init_branch && \
git checkout main && \
git merge init_branch
The error I'm seeing:
! [rejected] init_branch -> init_branch (fetch first)
error: failed to push some refs to 'https://github.com/my_github/my_repo.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.
On occasions getting this error (not sure why):
error: pathspec 'main' did not match any file(s) known to git
What I've tried:
- Switching the default branch in Github from
main
tomaster
and pushing straight tomaster
- that worked but want to keep it asmain
. - Not creating a new branch and attempting to push straight to
main
- doesn't work. - Having
git remote add origin ...
straight aftergit init
- doesn't work. - Removed the
symbolic-ref
line, addedgit config --global init.defaultBranch main
andgit pull origin main
. New error:
* branch main -> FETCH_HEAD
* [new branch] main -> origin/main
error: The following untracked working tree files would be overwritten by merge:
.gitignore
Please move or remove them before you merge.
Aborting
Any help would be much appreciated
CodePudding user response:
TL;DR
Make sure you create an empty repository on GitHub. Don't put in that initial commit. Then you can get rid of most of this stuff: you'll just create your new repository, commit to create main
(and rename to main
if necessary), do a git remote add origin url
, and git push origin main
.
(You can then, if you wish, use git remote set-head origin --auto
, although I personally find no value here.)
Long
OK, first, don't do this at all:
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main && \
It's not inherently wrong but it has no value.1 It has nothing to do with the errors you're getting, but removing it gets one more step out of the way and removes distractions from your view.
With that said, I think your problem stems from a fundamental misunderstanding of Git: of what it does, why, and how. To fix that, let's take a quick overview of Git repositories.
1I'm of the opinion that the existence of refs/remotes/origin/HEAD
itself has no value to begin with, but even if you disagree, you should be using git remote
or git ls-remote
to obtain their symbolic HEAD
here, rather than hard-coding main
. Your remote-tracking names are supposed to reflect their branch names as they exist now, not as you wish they existed.