Leaving Github, how to change the origin of a Git repo?
Asked Answered
C

6

56

I'm hosting a project in Github, but now I purchased a plan with Dreamhost that includes shell access and Git.

      Github [Origin]
       /         \
  pull/           \pull
     /push     push\
    /               \  
Laptop           Dreamhost
(cloned)          (cloned)

I would like to delete my repo from Github, and starting push directly to DH.

How do I change origin in my Laptop, and should I delete the origin in Dreamhost?

Creepie answered 10/6, 2010 at 3:14 Comment(0)
I
127
git remote rename origin github
git remote add origin <DreamHost-git-URL>
# test and make sure the DreamHost origin works properly for fetch and push
git remote rm github

I prefer using the 'git remote' command instead of screwing about with the .git/config file by hand.

Ikey answered 10/6, 2010 at 3:23 Comment(7)
I agree. I consider the format of .git/config an implementation detail, I always use the tools like git remote and git config to make sure I don't screw up the syntax.Septic
Oh +1 because I like this. But you should add further infos how to migrate the tracking branches afterwards...Friedrich
I had to add git config branch.master.remote origin. It seems like git changed that variable as a side effect of the remote rename.Fulbright
I had to run following commands: git config branch.master.remote origin git config branch.master.merge refs/heads/masterAsteroid
@DanielLuna thanks for your comment, I added this to the answer.Huh
Also after doing the steps that @Asteroid mentioned I had to push origin master on the first push to create a master branch on the new bare repo. After than git push worked fine.Dickman
Easier way is: git remote set-url origin <new-url>Dynamometer
M
58

The easiest way is:

$ git config remote.origin.url <Dreamhost-git-URL>

You show the remotes after this:

$ git remote -v
origin Dreamhost-git-URL (fetch)
origin Dreamhost-git-URL (push)
Mi answered 12/4, 2011 at 12:47 Comment(2)
Yep, seems the best solution.Hist
I don't think this solution existed in the released versions of git at the time I wrote my answer, but it definitely seems easier. :)Ikey
L
28

The best way is to git remote set-url origin <new-url>

Lima answered 8/6, 2012 at 15:34 Comment(2)
this comment should be top. the only one-liner that doesn't depend on git's internal config structureLeff
I agree, I think this is the best answer. It's simple and does what you need in one line.Windermere
T
26

The easiest way is to edit your .git/config file on your laptop. Simply search for your github url and replace it with the one from DreamHost. Make sure that your ssh public key is set on Dreamhost. Now you can push origin master and you will have populated your dreamhost repository.

You can delete origin on dreamhost but there is no need.

Also, ensure that the repository that is there is bare. By default, you cannot push to non-bare repositories.

Terrapin answered 10/6, 2010 at 3:20 Comment(6)
Well, as you said, it's a non-bare repo, I'm a little confused. What to do now ?Creepie
Found #1785006Creepie
As above, all you need is the --bare option when cloning what is going to be an upstream repo.Terrapin
Well, you CAN push to non-bare repos - but you REALLY SHOULD NOT!Friedrich
thanks for the advice. i thought it would be as easy as changing the config, just needed confirmation.Molal
A comment from a suggested edit: By default, you cannot push to non-bare repositories. (at least not to the branch that is currently checkouted there).Aidaaidan
T
4

The easiest way is to edit your .git/config file, which lists where the origin lives. You can test it by running a git fetch

You can delete the remote references on the Dreamhost side if you like, in the same file.

Truncheon answered 10/6, 2010 at 3:21 Comment(0)
C
2

why not simply :

git remote remove origin

git remote add origin <Dreamhost-git-URL>

git push -u origin --all --tags
Carleycarli answered 19/6, 2014 at 21:13 Comment(3)
I had created an empty repository on github. On local setup, I had cloned from the repository, then added some files and pushed to the github repository. Then I created a fresh repository on gitlab, and imported code from the github repository. Now I want to change the remote repository reference of my local repository to this gitlab repository. I did git remote add origin <gitlab-URL> on my local repository. It did not not show any error (am I supposed to get some result? I did not get any success message either). But, now I am not able to see changes made in the gitlab repo on my local.Goon
I have tried all the suggestions but I don't have the tags in the new origin. How I have to do for moving the tags too?Jellyfish
@StefanoBossi : git push --tags ?Carleycarli

© 2022 - 2024 — McMap. All rights reserved.