5

I want to create a git repository on my USB stick. On the stick I created a bare git repository by using the command

git init --bare MyRepo

and in the repository on the laptop (Ubuntu 14.04) I issued the following commands

git init
git add .
git commit -m 'first commit'
git remote add origin /media/alexander/Stick/MyRepo

and got the error

fatal: remote origin already exists.

I successfully created a git repo on the stick just before without any error (using different directories of course). Now I get this error. The output of 'git status' is

On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

So what is going wrong?

Alex
  • 38,938
  • 74
  • 207
  • 406
  • 1
    Make sure you didn't run `git init` in the wrong directory (in an already existing Git repo). – axiac Oct 08 '17 at 08:14
  • Possible duplicate of [Remote origin already exists on 'git push' to a new repository](https://stackoverflow.com/questions/1221840/remote-origin-already-exists-on-git-push-to-a-new-repository) – phd Oct 08 '17 at 13:29

1 Answers1

18

Remove the remote origin first, then add again with the path.

$ git remote rm origin
$ git remote add origin <repo-url> 

Another way: Set the origin's url instead of adding.

$ git remote set-url origin <repo-url>
Sajib Khan
  • 20,492
  • 6
  • 56
  • 69
  • Yes, maybe that worked. But how did I get into this problem in first place? – Alex Oct 08 '17 at 07:26
  • 1
    I guess, `.git` folder already existed when you did `git init` command. Can you check the output of `git init` command? if output like **"Reinitialized existing Git ..."** then .git already existed and `origin` existed also. – Sajib Khan Oct 08 '17 at 07:33
  • Also, before doing this, it could be interesting to check `git remote -v` and `git remote show origin` to see what might be there. – dvaergiller Apr 30 '18 at 11:36