How to change the URI (URL) for a remote Git repository?

前端 未结 25 2066
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 00:28

I have a repo (origin) on a USB key that I cloned on my hard drive (local). I moved \"origin\" to a NAS and successfully tested cloning it from here.

I would like to

相关标签:
25条回答
  • Change Host for a Git Origin Server

    from: http://pseudofish.com/blog/2010/06/28/change-host-for-a-git-origin-server/

    Hopefully this isn’t something you need to do. The server that I’ve been using to collaborate on a few git projects with had the domain name expire. This meant finding a way of migrating the local repositories to get back in sync.

    Update: Thanks to @mawolf for pointing out there is an easy way with recent git versions (post Feb, 2010):

    git remote set-url origin ssh://newhost.com/usr/local/gitroot/myproject.git
    

    See the man page for details.

    If you’re on an older version, then try this:

    As a caveat, this works only as it is the same server, just with different names.

    Assuming that the new hostname is newhost.com, and the old one was oldhost.com, the change is quite simple.

    Edit the .git/config file in your working directory. You should see something like:

    [remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://oldhost.com/usr/local/gitroot/myproject.git
    

    Change oldhost.com to newhost.com, save the file and you’re done.

    From my limited testing (git pull origin; git push origin; gitx) everything seems in order. And yes, I know it is bad form to mess with git internals.

    0 讨论(0)
  • 2020-11-22 01:16

    An alternative approach is to rename the 'old' origin (in the example below I name it simply old-origin) and adding a new one. This might be the desired approach if you still want to be able to push to the old origin every now and then:

    git remote rename origin old-origin
    git remote add origin git@new-git-server.com>:<username>/<projectname>.git
    

    And in case you need to push your local state to the new origin:

    git push -u origin --all
    git push -u origin --tags
    
    0 讨论(0)
  • 2020-11-22 01:17
    git remote -v
    # View existing remotes
    # origin  https://github.com/user/repo.git (fetch)
    # origin  https://github.com/user/repo.git (push)
    
    git remote set-url origin https://github.com/user/repo2.git
    # Change the 'origin' remote's URL
    
    git remote -v
    # Verify new remote URL
    # origin  https://github.com/user/repo2.git (fetch)
    # origin  https://github.com/user/repo2.git (push)
    

    Changing a remote's URL

    0 讨论(0)
  • 2020-11-22 01:18
    git remote set-url origin git://new.location
    

    (alternatively, open .git/config, look for [remote "origin"], and edit the url = line.

    You can check it worked by examining the remotes:

    git remote -v
    # origin  git://new.location (fetch)
    # origin  git://new.location (push)
    

    Next time you push, you'll have to specify the new upstream branch, e.g.:

    git push -u origin master
    

    See also: GitHub: Changing a remote's URL

    0 讨论(0)
  • 2020-11-22 01:18

    You can change the url by editing the config file. Go to your project root:

    nano .git/config
    

    Then edit the url field and set your new url. Save the changes. You can verify the changes by using the command.

    git remote -v 
    
    0 讨论(0)
  • 2020-11-22 01:18

    If your repository is private then

    1. Open Control Panel from the Start menu
    2. Select User Accounts
    3. Select "Manage your credentials" in the left hand menu
    4. Delete any credentials related to Git or GitHub

    Reference

    0 讨论(0)
提交回复
热议问题