Leaving Github, how to change the origin of a Git repo?
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.
Ben Orozco
Software engineer specialized in leading technical teams and full stack web development. Passionate about launching end-to-end & 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. & LATAM.
Updated on July 15, 2022Comments
-
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 almost 14 yearsWell, as you said, it's a non-bare repo, I'm a little confused. What to do now ?
-
Ben Orozco almost 14 years
-
Adam Dymitruk almost 14 yearsAs above, all you need is the --bare option when cloning what is going to be an upstream repo.
-
Jörg W Mittag almost 14 yearsI agree. I consider the format of
.git/config
an implementation detail, I always use the tools likegit remote
andgit config
to make sure I don't screw up the syntax. -
hurikhan77 almost 14 yearsWell, you CAN push to non-bare repos - but you REALLY SHOULD NOT!
-
hurikhan77 almost 14 yearsOh +1 because I like this. But you should add further infos how to migrate the tracking branches afterwards...
-
Daniel Luna almost 13 yearsI 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 about 12 yearsI had to run following commands:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
-
Thomas over 11 years@DanielLuna thanks for your comment, I added this to the answer.
-
thescientist over 11 yearsthanks for the advice. i thought it would be as easy as changing the config, just needed confirmation.
-
John Dvorak over 11 yearsA 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 about 11 yearsYep, seems the best solution.
-
Daniel almost 11 yearsAlso 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 thangit push
worked fine. -
Bouke Versteegh over 8 yearsthis comment should be top. the only one-liner that doesn't depend on git's internal config structure
-
clee over 8 yearsI 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 about 8 yearsI agree, I think this is the best answer. It's simple and does what you need in one line.
-
Sandeepan Nath about 8 yearsI 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 over 3 yearsI 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 over 3 years@StefanoBossi : git push --tags ?