5

I'm relatively new to git and I think I've painted myself into a corner. In order to test some functionality; I copied and pasted over a repo directory locally (I know - absolute wrong thing to do. I wasn't thinking). I have since been working and creating branches - progress that I'd like to keep. Of course, when I tried to set up a new repo and push to it... my local repo wants to push to the old remote.

Is there a way to change the name of the existing local repo (that I copied) to the name of the newly created, empty, remote one so that when I do a push, it will go to the new remote? I don't want to overwrite the file since I need to keep the branches.

I have tried:

git remote add origin https://github.com/my-github-username/mygithubrepo.git

...thinking that I could overwrite the repository url with the new info - but it gave me the error:

fatal: remote origin already exists.

...Which in hindsight is a good thing. I'm glad this didn't work, as I DO NOT want to modify the original remote repo in any way whatsoever. Like I said... I'm new to git.

Any advice or help would be greatly appreciated. Thanks in advance!

user984464
  • 67
  • 2
  • 7
  • I don't quite understand what your problem is or how copying a directory caused it (I think it's often fine to copy git repos as directories?), but if all you need to do is change the URL of `origin`, you should be able to just use `git remote remove` before `git remote add` (or maybe `git remote set-url`?) See [`git help remote`](https://git-scm.com/docs/git-remote). – Dan Getz Mar 29 '16 at 17:07
  • The name of a repo is merely a local setting. You can give it whatever name you want. The URL is the important piece. I usually have two repos: personal (points to my fork) and upstream (points to the "central" repo) – Joe Phillips Mar 29 '16 at 17:09
  • Thanks for the reference, @DanGetz . I was trying to point my local, copied repo to a new remote location without destroying the new branches I'd created. All of the settings were for the old repo because I copied, pasted, and renamed the local folder manually. I managed to do it successfully by editing a couple hidden files. Not preferred, but it worked. I'll add the solution below. – user984464 Mar 29 '16 at 17:56
  • Thanks for the advice, @JoePhilllips! – user984464 Mar 29 '16 at 17:57

1 Answers1

8

Is there a way to change the name of the existing local repo (that I copied) to the name of the newly created...

You have to change the url : (replace the add with set-url)

git remote set-url origin https://github.com/my-github-username/mygithubrepo.git
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Hi @codewizard, I did fix the solution by editing the config and FETCH_HEAD files in the new directory manually. Once I ran git push origin master - with my branches in tact - the new local repo was pushed to the new remote repo I'd previously created. Is this the same thing your suggestion accomplishes? Forgive me for asking for clarity; but after reading the reference docs that Dan posted the link to, I was still really nervous about accidentally effecting (renaming) the old remote repo. So I did a search for references to the old repo in the hidden .git folder and edited the files. – user984464 Mar 29 '16 at 18:08
  • If so, I can select your answer as the solution. I'm also looking at other explanations of 'git remote set-url' so I'm perfectly clear about what it does. – user984464 Mar 29 '16 at 18:11
  • 1
    Hi again. What the set-url does it very simple. its updating the url of teh project. if you will edit `.git/config` you will see the url in there. This command update the url which can be done manually as well as explained in the previous line (by editing the file) – CodeWizard Mar 29 '16 at 18:19
  • 1
    FETCH_HEAD is something diffrent - its is a short-lived ref, to keep track of what has just been fetched from the remote repository (commit id) – CodeWizard Mar 29 '16 at 18:20
  • 1
    If you interested to understand what is HEAD read this out (feel free to vote as well :-)) http://stackoverflow.com/questions/34519665/how-to-move-head-back-to-a-previous-location/34519716#34519716 – CodeWizard Mar 29 '16 at 20:06
  • Great! Thank you! None of my votes have counted thus far as I still have a very low reputation score - but as soon as I can get to 15, you'll have an upvote :) – user984464 Mar 30 '16 at 14:17
  • Voted for your q as well – CodeWizard Mar 30 '16 at 14:20
  • Thank you! Now all of my upvotes are made public! Cool :) – user984464 Mar 30 '16 at 21:47