184

I know similar questions have already been asked.

But, I believe my issue is due to a mistake I have previously made and therefore is different: let me explain.

Everything was working smoothly, as I could:

  • git add . all the files from my local repository.
  • git commit -m "message here" to add messages to my commits.
  • git push origin master to upload my files to GitHub.
  • git push heroku master to upload my files to Heroku.

However, at some point, I created a new branch locally called add-calendar-model in case next steps of the app development would go south...

... which is exactly what happened.

However, despite many attempts, I did not manage to get the initial code — i.e. the code from before I created the new branch — from the master branch to my local repository.

So, I decided to manually delete all the files from my local repository and git clone my master branch from GitHub.

This way, I got all my files back, but now, I cannot push any more to the remote repository.

Any time I try to run git push origin add-calendar-model or git push origin master, I get the following error:

fatal: 'origin' 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.

I am not very comfortable with Git and GitHub, as you may have guessed by now, and I have to admit that I have no clue about how to fix this.

Any idea?

Thibaud Clement
  • 6,607
  • 10
  • 50
  • 103
  • 2
    I had a similar error, But my problem was I had initialized git in the parent directory of the current folder I was trying it. Just in case if anyone is still facing, can look where the git is initialized and then try again. – Himanshu Kriplani Apr 17 '20 at 02:39

17 Answers17

321

First, check that your origin is set by running

git remote -v

This should show you all of the push / fetch remotes for the project.

If this returns with no output, skip to last code block.

Verify remote name / address

If this returns showing that you have remotes set, check that the name of the remote matches the remote you are using in your commands.

$git remote -v
myOrigin ssh://git@example.com:1234/myRepo.git (fetch)
myOrigin ssh://git@example.com:1234/myRepo.git (push)

# this will fail because `origin` is not set
$git push origin main

# you need to use
$git push myOrigin main

If you want to rename the remote or change the remote's URL, you'll want to first remove the old remote, and then add the correct one.

Remove the old remote

$git remote remove myOrigin

Add missing remote

You can then add in the proper remote using

$git remote add origin ssh://git@example.com:1234/myRepo.git

# this will now work as expected
$git push origin main
Simo Pelle
  • 141
  • 1
  • 11
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • It worked for me without the `ssh://` in front of `git@example.com:1234/myRepo.git` – Carol-Theodor Pelu Jul 16 '18 at 07:05
  • I was reading this question if you could help with the new repository push error as well? – StaticVariable Sep 03 '18 at 07:04
  • I renamed my remote branch from upstream to origin and it caused the error, after completely deleting the remote reference using `git remote remove origin` and then adding it again using `git remote add origin ` then it worked fine – Uriahs Victor Jun 29 '21 at 18:31
  • This worked for me, with a slight tweak. git remote -v was showing that my origin was git@bitbucket.org/[repo].git. After removing that origin, I replaced it with just https://bitbucket.org/[repo].git, and this worked. To clarify, I got rid of the "git@" and just put in the repo URL without a user, prompting it to ask for a user for all push/pull commands (which is what I needed anyway). – Bad Programmer Aug 03 '22 at 17:59
32

It works for me.

git remote add origin https://github.com/repo.git
git push origin master

add the repository URL to the origin in the local working directory

vinod
  • 535
  • 5
  • 10
  • 2
    When I removed the ```*.git``` in the end it worked for me: ```git remote add origin https://github.com/user/repo``` – Joel Wiklund Nov 05 '22 at 07:12
16

As Matt Clark stated above

However, origin might not be set, so skip the deleting step and simply attempting to add can clear this up.

git remote add origin <"clone">

Where "clone" is simply going into your GitHub repo and copying the "HTTPS clone URL" and pasting into GitBash

heb-NR
  • 317
  • 5
  • 13
11

This is the way I updated the master branch

This kind of error occurs commonly after deleting the initial code on your project

So, go ahead, first of all, verify the actual remote version, then remove the origin add the comment, and copy the repo URL into the project files.

$ git remote -v
$ git remote rm origin
$ git commit -m "your commit"
$ git remote add origin https://github.com/user/repo.git
$ git push -f origin master
Jose Antonio
  • 840
  • 14
  • 25
  • 1
    Try adding explanations to your answer in a way that addresses the question and attempts to help a reader understand. Currently, it just reads like some anecdote and wouldn't even work in the general case (e.g. `https://github.com/` is the website root and a git repo) – Kache Jul 07 '20 at 05:38
5

if you add your remote repository by using git clone then follow the steps:-

git clone <repo_url> then

git init

git add * *means add all files

git commit -m 'your commit'

git remote -v for check any branch run or not if not then nothing show then we add or fetch the repository. "fetch first". You need to run git pull origin <branch> or git pull -r origin <branch> before a next push.

then

git remote add origin <git url>
 git pull -r origin master
git push -u origin master```
md sha
  • 51
  • 1
  • 1
3

Make sure the config file at .git is correct...Check URL & Make sure your using the correct protocol for your keys ...ProjectWorkspace/.git/config

  ~Wrong url for git@bitbucket
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = gitbucket.org:Prezyack/project-one-hello.git
    fetch = +refs/heads/*:refs/remotes/origin/*

 ~Wrong URL for SSH...
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = https://emmap1@bitbucket.org/emmap1/bitbucketspacestation.git
[branch "master"]
    remote = origin
    merge = refs/heads/master

We are looking at the URL... e.g: For bitbucket, expect git@bitbucket.org....If its gitbucket.org. make the necessary changes.. SAVE Try pushing again.

Magere
  • 99
  • 6
2

These two steps worked for me!

Step 1:

git remote set-url origin https://github.com/username/example_repo.git

Step 2:

git push --set-upstream -f origin main

Step 3:

your username and password for github

On step 2, -f is actually required because of the rebase, quote from this post.

BlueJapan
  • 1,286
  • 2
  • 15
  • 16
2

For me it helps just by removing existing origin (after i created a new repo and few steps). But before check if the origin is exist in your case:

git remote -v

Then check name of origin (usually set by default) and remove it.

git remote remove origin

And use common github commands while pushing repository to add new origin and finally push your code:

git remote add origin https://github.com/username/repository.git
git branch -M main

git push -u origin main
1

A similar error appears while pulling the changes from the origin. If you are trying in Intellij from the menu options, the pull might not work directly.

Go to terminal and type this command and this should work out: git pull origin master

1

What fixed this for me was re-setting my origin url:

git remote set-url origin https://github.com/username/example_repo.git

And then I was able to successfully git push my project. I had to do this even though when I viewed my origins with git remote -v, that the urls were same as what I re-set it as.

deesolie
  • 867
  • 7
  • 17
1

Most probably the issue is that your remote origin is not set.

git add .
git commit -m "Your commit message"
git remote add origin https://repositoryurlpath.git
git push origin master

Extra Tips:

Check if the remote origin is set

git remote -v

Reset the remote origin

git remote remove origin
git remote add origin https://repositoryurlpath.git
Erielama
  • 111
  • 1
  • 3
0

I had the same issue. When I checked my config file I noticed that 'fetch = +refs/heads/:refs/remotes/origin/' was on the same line as 'url = Z:/GIT/REPOS/SEL.git' as shown:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = Z:/GIT/REPOS/SEL.git     fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[gui]
    wmstate = normal
    geometry = 1109x563+32+32 216 255

At first I did not think that this would have mattered but after seeing the post by Magere I moved the line and that fixed the problem:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = Z:/GIT/REPOS/SEL.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[gui]
    wmstate = normal
    geometry = 1109x563+32+32 216 255
Beaug45
  • 1
  • 2
0

It happens when you push your code from the current location but after cloning any projects Git creates its own different folder so we have to change our current directory to the required directory. If any got these issue. We can solve it by following these easy steps:-

  1. Firstly create an empty folder.
  2. Open Git GUI/Bash or CMD in the empty folder. Open the empty folder and right click and then open Git.
  3. Click on a clone in Bitbucket(after creating your repository) and copy the cloning path of your repository.
  4. Paste it into your Git and Enter.
  5. After cloning, a new folder is created by git.
  6. Change your directory to the new folder that is created by Git after cloning your repository.
  7. Paste/puts your desired projects/files/folder in this folder.
  8. After keeping your all projects files. Again open Git GUI/Bash in the current folder.
  9. Then type these as usual: a.git add --all b. git commit -m "Your Desired Heart Sound" c. git push -u origin master.
  10. Finally after following these steps you push your projects to Bitbucket

Error:-fatal: Not a git repository (or any of the parent directories):

Thanks.

Naffy Kausar
  • 91
  • 2
  • 2
  • 9
0

Firstly remote verbose with the following command:

git remote -v

And see the result of something like that:

origin  https://github.com/MahfuzKhandaker/StudyOnline.git (fetch)
origin  https://github.com/MahfuzKhandaker/StudyOnline.git (push)

Then you have to run the following command to remove origin:

git remote remove origin

And finally you have to add origin:

git remote  add origin  https://github.com/MahfuzKhandaker/StudyOnline.git

Then run the command:

git branch -M main
git push -u origin main

Hope this helps!

Mahfuz Khandaker
  • 454
  • 5
  • 12
0

You can see the multiple ways to solve but I recommended to trying this first. It works for me perfectly.

    step 1. git remote -v 
    step 2. git remote rm origin
    step 3. git add .
    step 4. git commit -m "your commit"
    step 5. git remote set-url origin https://github.com/<your_github_username>/<repository_name>.git
    step 6. git push --set-upstream -f origin main

Note: if error: No such remote 'origin' this error occurred. then before step 5 use this command -

git remote add origin https://github.com/<your_github_username>/<repository_name>.git

then again start from step 5


Follow this 6 steps, Thanks.


0

you just need add github.com to your host file:

ssh-keygen -f "/home/<user>/.ssh/known_hosts" -R "github.com"
Jaber Alshami
  • 336
  • 3
  • 14
-4

Sometimes you don't have a local REF for pushing that branch back to the origin.
Try

git push origin master:master

This explicitly indicates which branch to push to (and from)

vsriram92
  • 593
  • 1
  • 4
  • 15