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

前端 未结 25 2489
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  不要未来只要你来
    2020-11-22 01:00

    Change remote git URI to git@github.com rather than https://github.com

    git remote set-url origin git@github.com:/.git
    

    Example:

    git remote set-url origin git@github.com:Chetabahana/my_repo_name.git
    

    The benefit is that you may do git push automatically when you use ssh-agent :

    #!/bin/bash
    
    # Check ssh connection
    ssh-add -l &>/dev/null
    [[ "$?" == 2 ]] && eval `ssh-agent`
    ssh-add -l &>/dev/null
    [[ "$?" == 1 ]] && expect $HOME/.ssh/agent
    
    # Send git commands to push
    git add . && git commit -m "your commit" && git push -u origin master
    

    Put a script file $HOME/.ssh/agent to let it runs ssh-add using expect as below:

    #!/usr/bin/expect -f
    set HOME $env(HOME)
    spawn ssh-add $HOME/.ssh/id_rsa
    expect "Enter passphrase for $HOME/.ssh/id_rsa:"
    send "\n";
    expect "Identity added: $HOME/.ssh/id_rsa ($HOME/.ssh/id_rsa)"
    interact
    

提交回复
热议问题