1448

I just did git init to initialize my folder as Git repository and then added a remote repository using git remote add origin URL. Now I want to remove this git remote add origin and add a new repository git remote add origin new-URL. How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Om3ga
  • 30,465
  • 43
  • 141
  • 221

16 Answers16

2678

Instead of removing and re-adding, you can do this:

git remote set-url origin git://new.url.here

See this question: How to change the URI (URL) for a remote Git repository?

To remove remote use this:

git remote remove origin
Dmitriy
  • 5,357
  • 8
  • 45
  • 57
kahowell
  • 27,286
  • 1
  • 14
  • 9
  • 11
    @acannon828, the protocol necessary depends on how you're connecting to git. The example provided assumes you are using the git protocol. The [git book](http://git-scm.com/book/en/Git-on-the-Server-The-Protocols) explains various protocols supported by git. – kahowell Sep 26 '14 at 00:57
  • 3
    This is the correct answer, there is some confusion caused by the phrasing of the title and the question itself. – Ian Lewis Oct 22 '14 at 15:27
  • 3
    If you use Bitbucket instead of github you will delete the first "git://" part and directly write git@bitbucket.org:yourusername/reponame.git and of course change the place holders : "yourusername" and "reponame" with yours. – Recomer Feb 06 '16 at 08:32
  • 1
    This is not the correct answer, the correct answer is: git remote set-url origin new.url.here. Having git:// will just cause a fatal error. Not sure why this was in the answer. – Linguistics Dec 31 '21 at 21:43
807

If you insist on deleting it:

git remote remove origin

Or if you have Git version 1.7.10 or older

git remote rm origin

But kahowell's answer is better.

Community
  • 1
  • 1
1615903
  • 32,635
  • 12
  • 70
  • 99
115

To remove a remote:

git remote remove origin

To add a remote:

git remote add origin yourRemoteUrl

and finally

git push -u origin master
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Vontei
  • 1,727
  • 2
  • 14
  • 16
48

you can try this out,if you want to remove origin and then add it:

git remote remove origin

then:

git remote add origin http://your_url_here
Manish Kakati
  • 657
  • 5
  • 12
41

I don't have enough reputation to comment answer of @user1615903, so add this as answer: "git remote remove" does not exist, should use "rm" instead of "remove". So the correct way is:

git remote rm origin
heroin
  • 2,148
  • 1
  • 23
  • 32
41

if multiple remotes are set for a project like heroku and own repository then use the below command to check the available remote URLs inside the local project directory

git remote -v

it will display all the remote URLs like

heroku  https://git......git
origin  https://git......git

if you want to remove heroku remote then,

git remote remove heroku

it will remove heroku remote only if want to remove own remote repository

git remote remove origin
32

To remove just use this command

git remote remove origin

Add new

git remote add origin (path)
samran
  • 581
  • 6
  • 7
  • just make sure to keep checking your current remote repository address using "git remote -v" otherwise you might end up removing the wrong remote repository. – cristiandatum Apr 15 '22 at 09:12
25

You can rename (changing URL of a remote repository) using :

git remote set-url origin new_URL

new_URL can be like https://github.com/abcdefgh/abcd.git

Too permanently delete the remote repository use :

git remote remove origin
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Anshul Bisht
  • 1,644
  • 20
  • 21
20

To set a origins remote url-

   git remote set-url origin git://new.url.here

here origin is your push url name. You may have multiple origin. If you have multiple origin replace origin as that name.

For deleting Origin

   git remote rm origin/originName
   or
   git remote remove origin/originName

For adding new origin

   git remote add origin/originName git://new.url.here / RemoteUrl
Nasir Khan
  • 753
  • 1
  • 9
  • 22
16

Another method

Cancel local git repository(Warning: This removes the history)

rm -rf .git

Then; Create git repostory again

git init

Then; Repeat the remote repo connect

git remote add origin REPO_URL

A warning though: This removes the history.

Yasin UYSAL
  • 571
  • 6
  • 11
  • worked like a charm for me. It's not the prettiest way of doing it, but the git remote rm was not working ... and the suggestion at the github page didn't work either. Thanks – Samuel Aiala Ferreira Oct 17 '18 at 18:55
  • 1
    but then you loose all the history, right? in that case, why not pull in the code from the other/new repository? – RobMac Sep 01 '19 at 21:30
  • 4
    yes, it does remove the whole history. @Yasin should add some kind of warning with the answer. – Amrit Shrestha Jan 26 '20 at 17:21
  • Yeah, good solution if you want to also get rid of remote history, otherwise stick to other answers :) – Marko Jun 11 '21 at 20:00
  • 1
    I do not recommend doing this, if you want to maintain the commit history – serup Jan 18 '22 at 09:01
16

perhaps I am late you can use git remote remove origin it will do the job.

Krishna Kamal
  • 751
  • 2
  • 10
  • 21
8

first will change push remote url

git remote set-url --push origin https://newurl

second will change fetch remote url

git remote set-url origin https://newurl
Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
5

You can go to the .git folder, edit the config file without using the commands.

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Shusen Yi
  • 779
  • 7
  • 6
2

Git aliases has been life saver:

Note: Default name origin if it is different than update according to your needs. I usually have "origin" for all repos

Step-1: Define git aliases ->

This command will help to view your existing "origin" and remote "URL"

 git config --global alias.url "remote -v" 

This will remove your existing remote "origin"

git config --global alias.ro "remote remove origin"

This will add new remote "origin"

git config --global alias.ao "remote add origin"

Step-2: How to use it ->

  • open your terminal having git repo
  • check existing origin/ url by running command
git url

e.g output:

IF-PERSONAL REPO:

git@github.com:<USERNAME>/<REPO-NAME>.git (fetch/push)


IF-ORGANIZATION:

origin  git@github.com:<ORGANIZATION>/<REPO-NAME>.git (fetch/push)

  • Remove existing origin and url by running command
git ro
  • Add new remote origin by running command
git ao <URL>

e.g git ao git@github.com:<USERNAME>/<REPO-NAME>.git 

khizer
  • 1,242
  • 15
  • 13
1
Well, This method and technique worked fine for me: 
Inside the .git folder of your project directory, change these files: 
1 -> configs file 
  -> open it up 
  -> change the ref URL to the remote one. 
 (You must also set your remote origin 
  branch the same as the local 
  branch here inside this file. e.g: remote: main, local: main
 ) 
2 -> git fetch 
3 -> .git 
  -> refs 
  -> heads && remotes folder 
  -> make sure both in files, origins are the same inside both heads and 
    remotes folders. e.g: main or master
4 -> .git 
  -> refs 
  -> remotes 
  -> main 
  -> open it up: 
   Copy the content and paste it inside the main file of the heads 
   folder.

Finally: 
Git fetch && git pull && git push 
Ahmad Reza Azimi
  • 534
  • 5
  • 16
0

None of the answers worked for me as I had a global origin set, which seemed to override everything in all my repos. In the end, I just edited the .gitconfig file in C:\Users\user_name.

Rob Sedgwick
  • 4,342
  • 6
  • 50
  • 87