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

29,402

Solution 1

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.

Solution 2

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)

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
29,402
Ben Orozco
Author by

Ben Orozco

Software engineer specialized in leading technical teams and full stack web development. Passionate about launching end-to-end &amp; customer-driven software products, coaching engineers and leveraging technology to tackle real-world challenges. Former CTO of SaaS startups with thousands of users in U.S. &amp; LATAM.

Updated on July 15, 2022

Comments

  • Ben Orozco
    Ben Orozco almost 2 years

    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?

  • Ben Orozco
    Ben Orozco almost 14 years
    Well, as you said, it's a non-bare repo, I'm a little confused. What to do now ?
  • Ben Orozco
    Ben Orozco almost 14 years
  • Adam Dymitruk
    Adam Dymitruk almost 14 years
    As above, all you need is the --bare option when cloning what is going to be an upstream repo.
  • Jörg W Mittag
    Jörg W Mittag almost 14 years
    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.
  • hurikhan77
    hurikhan77 almost 14 years
    Well, you CAN push to non-bare repos - but you REALLY SHOULD NOT!
  • hurikhan77
    hurikhan77 almost 14 years
    Oh +1 because I like this. But you should add further infos how to migrate the tracking branches afterwards...
  • Daniel Luna
    Daniel Luna almost 13 years
    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.
  • JoaoHornburg
    JoaoHornburg about 12 years
    I had to run following commands: git config branch.master.remote origin git config branch.master.merge refs/heads/master
  • Thomas
    Thomas over 11 years
    @DanielLuna thanks for your comment, I added this to the answer.
  • thescientist
    thescientist over 11 years
    thanks for the advice. i thought it would be as easy as changing the config, just needed confirmation.
  • John Dvorak
    John Dvorak over 11 years
    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).
  • Profpatsch
    Profpatsch about 11 years
    Yep, seems the best solution.
  • Daniel
    Daniel almost 11 years
    Also after doing the steps that @JoaoHornburg 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.
  • Bouke Versteegh
    Bouke Versteegh over 8 years
    this comment should be top. the only one-liner that doesn't depend on git's internal config structure
  • clee
    clee over 8 years
    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. :)
  • imns
    imns about 8 years
    I agree, I think this is the best answer. It's simple and does what you need in one line.
  • Sandeepan Nath
    Sandeepan Nath about 8 years
    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.
  • Stefano Bossi
    Stefano Bossi over 3 years
    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?
  • gjambet
    gjambet over 3 years
    @StefanoBossi : git push --tags ?