672

I have my project on GitHub at some location, git@github.com:myname/oldrep.git.

Now I want to push all my code to a new repository at some other location, git@github.com:newname/newrep.git.

I used the command:

git remote add origin git@github.com:myname/oldrep.git

but I am receiving this:

fatal: remote origin already exists.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
uzumaki naruto
  • 6,979
  • 3
  • 18
  • 12
  • 7
    Give the output of the command $> git remote -v show – sykora Aug 03 '09 at 11:50
  • possible duplicate of [Github "fatal: remote origin already exists"](http://stackoverflow.com/questions/10904339/github-fatal-remote-origin-already-exists) – That Brazilian Guy Feb 05 '14 at 14:52
  • 1
    A good way is to use **"import from another repository"** at the bottom of your new created repository, if you know the URL of the old one. – JW.ZG Jul 16 '16 at 04:58
  • A similar question was asked here: http://stackoverflow.com/questions/2432764/change-the-uri-url-for-a-remote-git-repository – jciloa Sep 27 '16 at 12:46
  • 1
    If you want to keep the original remote you can simply use a different name `git remote add origin2 ....`, *but* if you only need to push once without modifying the repo configuration then you can simply do `git push git@github.com:user/another-project.git master:master`. – ccpizza Jan 15 '17 at 18:41

22 Answers22

1051

You are getting this error because "origin" is not available. "origin" is a convention not part of the command. "origin" is the local name of the remote repository.

For example you could also write:

git remote add myorigin git@github.com:myname/oldrep.git  
git remote add testtest git@github.com:myname/oldrep.git

See the manual:

http://www.kernel.org/pub/software/scm/git/docs/git-remote.html

To remove a remote repository you enter:

git remote rm origin

Again "origin" is the name of the remote repository if you want to remove the "upstream" remote:

git remote rm upstream
MrHus
  • 32,888
  • 6
  • 31
  • 31
  • 13
    "git remote rm origin" didn't work from me, if it doesn't work for you try to check with "git remote -v" this will show you if your origin has a url set, if it doesn't likely you did the init locally and are trying to push it remote, and made a misstep like me). Then follow RobinH's answer: git remote set-url origin git@github.com:username/projectname.git – Clarence Liu May 23 '14 at 01:46
  • 1
    check [this answer](http://stackoverflow.com/questions/13572191/cannot-remove-remote-origin) in order to force the url. – srodriguex Jun 05 '14 at 21:32
  • 1
    "git remote rm origin" worked like a charm, wonderful! :) puhh, I'm new to the terminology of git so it was a bigger search, but your answer helped me a lot. :) thank you! – Martin Pfeffer Dec 09 '14 at 23:25
  • 5
    `git push -u origin master --force` – Ronnie Royston Aug 03 '16 at 20:50
  • 1
    --force is solution! – creator Mar 04 '18 at 04:35
  • --force is the solution when all else fails! (but you should avoid it) – lys Mar 01 '21 at 00:56
  • 1
    `git remote rm origin` worked for me – kkgarg Jan 25 '22 at 22:03
298

The previous solutions seem to ignore origin, and they only suggest to use another name. When you just want to use git push origin, keep reading.

The problem appears because a wrong order of Git configuration is followed. You might have already added a 'git origin' to your .git configuration.

You can change the remote origin in your Git configuration with the following line:

git remote set-url origin git@github.com:username/projectname.git

This command sets a new URL for the Git repository you want to push to. Important is to fill in your own username and projectname

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hoetmaaiers
  • 3,413
  • 2
  • 19
  • 29
  • 1
    This fiexd it for me. But what really help me with this problem was because I user portableGit that was installed with Gihub for windows. I found the solution [here](http://stackoverflow.com/a/17351255/1128695) – PerseP Mar 18 '14 at 19:57
  • 1
    Great! I needed change my gitorious repo to git lab repo and this solution was perfect! Thank you! – Marcelo dos Santos Apr 22 '15 at 23:12
  • 1
    Same situation as OP, and I needed to do this and then @MrHus's solution. – Sean the Bean Aug 21 '15 at 20:47
  • 2
    If anyone's getting a permission error, you might need to use the https version of this like I had to. It'll be like this: `git remote set-url origin https://github.com//.git` – Justin Liu Mar 27 '18 at 10:32
99

If you have mistakenly named the local name as "origin", you may remove it with the following:

git remote rm origin
Özgür
  • 8,077
  • 2
  • 68
  • 66
  • 1
    What does" mistakenly named the local name as 'origin'" actually mean?Can you explain it in detail?@Ozgur – Yuan Wen Jul 18 '16 at 05:43
  • 1
    This could mean that you added the remote origin that doesn't point to a git repository. Therefore, if you are planning to push all your changes to the master, the git will complain saying that the remote origin is not a git repository. – Gautham Honnavara Feb 25 '17 at 01:36
29

METHOD1->

Since origin already exist remove it.

git remote rm origin
git remote add origin https://github.com/USERNAME/REPOSITORY.git

METHOD2->

One can also change existing remote repository URL by ->git remote set-url

If you're updating to use HTTPS

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

If you're updating to use SSH

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

If trying to update a remote that doesn't exist you will receive a error. So be careful of that.

METHOD3->

Use the git remote rename command to rename an existing remote. An existing remote name, for example, origin.

git remote rename origin startpoint
# Change remote name from 'origin' to 'startpoint'

To verify remote's new name->

git remote -v

If new to Git try this tutorial->

TRY GIT TUTORIAL

Shaurya Uppal
  • 3,410
  • 31
  • 31
17

You can simply edit your configuration file in a text editor.

In the ~/.gitconfig you need to put in something like the following:

[user]
        name  = Uzumaki Naruto
        email = myname@example.com

[github]
        user = myname
        token = ff44ff8da195fee471eed6543b53f1ff

In the oldrep/.git/config file (in the configuration file of your repository):

[remote "github"]
        url = git@github.com:myname/oldrep.git
        push  = +refs/heads/*:refs/heads/*
        push  = +refs/tags/*:refs/tags/*

If there is a remote section in your repository's configuration file, and the URL matches, you need only to add push configuration. If you use a public URL for fetching, you can put in the URL for pushing as 'pushurl' (warning: this requires the just-released Git version 1.6.4).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
15

I had the same issue, and here is how I fixed it, after doing some research:

  1. Download GitHub for Windows, or use something similar, which includes a shell.
  2. Open the Git Shell from the task menu. This will open a power shell including Git commands.
  3. In the shell, switch to your old repository, e.g. cd C:\path\to\old\repository.
  4. Show the status of the old repository.
  • Type git remote -v to get the remote path for fetch and push remote. If your local repository is connected to a remote, it will show something like this:

     origin  https://user@bitbucket.org/team-or-user-name/myproject.git (fetch)
     origin  https://user@bitbucket.org/team-or-user-name/myproject.git (push)
    
  • If it's not connected, it might show origin only.

  1. Now remove the remote repository from the local repository by using

    git remote rm origin
    
  2. Check again with git remote -v, as in step 4. It should show origin only, instead of the fetch and push path.

  3. Now that your old remote repository is disconnected, you can add the new remote repository. Use the following to connect to your new repository:

Note: In case you are using Bitbucket, you would create a project on Bitbucket first. After creation, Bitbucket will display all required Git commands to push your repository to remote, which look similar to the next code snippet. However, this works for other repositories as well.

cd /path/to/my/repo # If you haven't done that yet.
git remote add mynewrepo https://user@bitbucket.org/team-or-user-name/myproject.git
git push -u mynewrepo master # To push changes for the first time.

That's it.

Dmitri
  • 2,658
  • 2
  • 25
  • 41
Michael
  • 3,982
  • 4
  • 30
  • 46
13
  1. git remote rm origin

  2. git remote -v It will not display any repository name

  3. git remote add origin git@github.com:username/myapp.git

  4. git push origin master It will start the process and creating the new branch. You can see your work is pushed to github.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
dev
  • 191
  • 1
  • 11
9
git remote rm origin
git remote add origin git@github.com:username/myapp.git
Aayushi
  • 787
  • 10
  • 14
9

The below two commands should help set up.

git remote set-url origin https://github.com/USERNAME/NEW_REPO.git
    
git push --set-upstream origin main
buddemat
  • 4,552
  • 14
  • 29
  • 49
Mansi Shah
  • 227
  • 2
  • 6
8

You don't have to remove your existing "origin" remote, just use a name other than "origin" for your remote add, e.g.

git remote add github git@github.com:myname/oldrep.git

mpelzsherman
  • 707
  • 9
  • 9
5

I had the same problem when I first set up using Bitbucket.

My problem was that I needed to change the word origin for something self-defined. I used the name of the application. So:

git remote add AppName https://someone@bitbucket.org/somewhere/something.git
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Murphy
  • 1,921
  • 2
  • 18
  • 21
4

You should change the name of the remote repository to something else.

git remote add origin git@github.com:myname/oldrep.git

to

git remote add neworigin git@github.com:myname/oldrep.git

I think this should work.

Yes, these are for repository init and adding a new remote. Just with a change of name.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nirvanastack
  • 455
  • 2
  • 5
  • 13
3

You could also change the repository name you wish to push to in the REPOHOME/.git/config file

(where REPOHOME is the path to your local clone of the repository).

nolim1t
  • 4,051
  • 1
  • 17
  • 7
2

You need to check the origin and add if not exists.

if ! git config remote.origin.url >/dev/null; then
    git remote add origin git@github.com:john/doe.git
fi

Create file check.sh, paste the script update your git repository URL and run ./check.sh.

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
2

I had the same issue but I found the solution to it. Basically "origin" is another name from where your project was cloned. Now the error

fatal: remote origin already exists.

LITERALLY means origin already exists. And hence to solve this issue, our goal should be to remove it. For this purpose:

git remote rm origin

Now add it again

git remote add origin https://github.com/__enter your username here__/__your repositoryname.git__

This did fix my issue.

1

This can also happen when you forget to make a first commit.

Clay Morton
  • 333
  • 2
  • 15
1

I just faced this issue myself and I just removed it by removing the origin. the origin is removed by this command

git remote rm origin

if you've added the remote repo as origin try implementing this command.

lys
  • 949
  • 2
  • 9
  • 33
1

Try to remove first existing origin, In order to see the which existing origin has registered with bash you can fire below command.

 git remote -v 

after you know the which version of origin has register with bash then you can remove existing origin by firing below command

git remote rm origin

Once you removed existing origin you can add new origin by firing below command in you case ..

git remote add origin git@github.com:myname/oldrep.git

Once you add your origin in git, then you can push your local commit to remote origin

git push -u origin --all
Pramod Lawate
  • 615
  • 1
  • 7
  • 21
0

Step:1

git remote rm origin

Step:2

git remote add origin enter_your_repository_url

Example:

git remote add origin https://github.com/my_username/repository_name.git
Sarath Chandran
  • 189
  • 1
  • 7
0

if you want to create a new repository with the same project inside the github and the previous Remote is not allowing you to do that in that case First Delete That Repository on github then you simply need to delete the .git folder C:\Users\Shiva\AndroidStudioProjects\yourprojectname\.git delete that folder,(make sure you click on hidden file because this folder is hidden )

Also click on the minus(Remove button) from the android studio Setting->VersionControl click here for removing the Version control from android And then you will be able to create new Repository.

Shivam Sharma
  • 63
  • 1
  • 10
0

Try this command it works for me.

rm -rf .git/

-1
git remote rm origin 

and then

git push -f 
Rasikh
  • 5
  • 1
  • 2
  • 1
    Are you sure that this works? Did you try it? – aaossa Mar 24 '22 at 18:04
  • yes, I have done many times – Rasikh Mar 25 '22 at 17:16
  • 1
    It does not seem to work for me, I get "fatal: No configured push destination". `git push -f ` uses "origin" as default remote location, but your previous command just deleted it. If this works under certain conditions, can you include those conditions in your answer? – aaossa Mar 26 '22 at 22:06