Abishek Ajai Satnur
Abishek Ajai Satnur

Reputation: 21

Git push error - failed to push some refs to <path>

I am trying to upload my code onto a repository I created on github but I get an error. Assuming the URL of the repository is https://github.com/a, how do I go about uploading my code to my remote repository?

I tried the following commands on git bash after navigating INSIDE the folder I want to upload -

git init
(output - Reinitializing existing Git repository in C://(path)/.git/

git add.
(blank output)

git commit -m "Initial commit" 
(output- On branch main. nothing to commit, working tree clean)

git remote add origin https://github.com/(path).git
(output - error: remote origin already exists) 

git push origin master
(output - error:src refspec master does not match any
error failed to push some refs to https://github.com/(path).git)

Why is this happening and what could I do to push my code to my remote git repo? (I want to learn how to use git via cmd)

Thanks for reading my query!

Upvotes: 1

Views: 24357

Answers (3)

cshalive
cshalive

Reputation: 1

I faced similar problem today. I guess, the issue is repo exists but not the branch. Try adding git branch -M main before pushing to main.

Here is the sequence for your reference:

git remote add origin [git url].git
git branch -M main
git push -u origin main

Upvotes: -2

Jim McBeath
Jim McBeath

Reputation: 95

You wrote:

git commit -m "Initial commit" 
(output- On branch main. nothing to commit, working tree clean)

git push origin master
(output - error:src refspec master does not match any

Your init created a branch called main but you tried to push master, which doesn't exist. Try pushing main instead.

Upvotes: 0

Marcio Rocha
Marcio Rocha

Reputation: 189

note that you already have an remote repository configured as origin you must remove this configuration or rename then add the correctly repo.

the git command to remove remotes repositories by name is git remote remove ${repo_name}, in your case:

git remote remove origin

the git command to rename remotes repositories is:

git remote rename ${old_repo_name} ${new_repo_name}

then you can add an new remote with the name origin

git remote add origin https://github.com/(path).git

you can assume that the git command git remote add origin https://github.com/(path).git says to git configure an new remote repository into the current local repository, this remote config will "point" to the <URL> and the "alias" of this <URL> will be "origin" (or the name you specify between add and <URL>).

in this case <URL>=https://github.com/(path).git

With this you can have various remotes and specify then when you execute git push ${repo_name}

You can set an "default" upstream for git pull/status with git push -u ${repo_name}

Upvotes: 2

Related Questions