0

I create a repository in Github and on my local machine I ran the code:

git init
git branch -m main
git remote add origin https://github.com/.../Project.git
git push -u origin main

Instead of using master branch I want to use main branch.

When running git push -u origin main I get the error:

failed to push some refs to 'https://github.com/.../Project.git'

What am I doing wrong?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • 1
    Is your local repo empty? I had to add a commit to run git push successfully. – Christian Nov 15 '22 at 22:44
  • 1
    It is not empty but I understood now that I first need to add some files and commit then before pushing. Now it is working. – Miguel Moura Nov 15 '22 at 22:47
  • This may be helpful: https://stackoverflow.com/questions/2337281/how-do-i-do-an-initial-push-to-a-remote-repository-with-git – Christian Nov 15 '22 at 22:48
  • That final message (`failed to push some refs...`) is a *summary* of the actual error(s), which all come *before* that line. That line itself is only interesting in that it means "please look above at previous lines". – torek Nov 22 '22 at 04:16

2 Answers2

2

Don't think you're doing anything necessarily wrong, persay. Git just doesn't know where to begin tracking between your local and the remote, because its a new repo (I assume). And you haven't initialised the remote yet. Try:

    git pull --rebase origin main
    git push origin main

Maybe do a git push -u origin main first. And make a copy of your source directory, just in case anything goes wrong so you don't lose your uncommitted code.

Whenever I am making a new repo on github, I use their website to create the repo first, then do a git clone to put the empty repo into my home directory. That way there is none of this nonsense when I go to make my first commit!

Noscere
  • 417
  • 3
  • 8
1

Is your local repo empty?

In this case, you need to add a file and commit it.

1. Run notepad test.txt (Windows) or touch test.txt (Mac)
2. Save the file.
3. git add . 
4. git commit -m "Initial commit"
5. git push -u origin main
Christian
  • 4,902
  • 4
  • 24
  • 42