0. Connecting to GitHub with SSH
https://help.github.com/articles/connecting-to-github-with-ssh/
1.Check that you are connecting to the correct server (SSH)
https://help.github.com/articles/error-permission-denied-publickey/
$ ssh -vT git@github.com
OpenSSH_5.6p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data /Users/you/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: Applying options for *
debug1: Connecting to github.com [IP ADDRESS] port 22.
(ssh 中的 -T 参数禁止分配伪终端。当用ssh或telnet等登录系统时,系统分配给我们的终端就是伪终端。
如果ssh使用此选项登录系统时,由于禁用,将无法获得终端;但仍能够获得shell,只不过看起来像在本地,也没有很多应有的环境变量,例如命令提示符,PS1等。//-T
不显示终端,只显示连接成功信息)https://segmentfault.com/q/1010000007607194
2.Using SSH over the HTTPS port
https://help.github.com/articles/using-ssh-over-the-https-port/
3.更换github链接URL(https/ssh)
https://help.github.com/articles/changing-a-remote-s-url/
3.1 Switching remote URLs from SSH to HTTPS
-
Open Git Bash.
-
Change the current working directory to your local project.
-
List your existing remotes in order to get the name of the remote you want to change.
git remote -v origin git@github.com:USERNAME/REPOSITORY.git (fetch) origin git@github.com:USERNAME/REPOSITORY.git (push)
-
Change your remote's URL from SSH to HTTPS with the
git remote set-url
command.git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
-
Verify that the remote URL has changed.
git remote -v # Verify new remote URL origin https://github.com/USERNAME/REPOSITORY.git (fetch) origin https://github.com/USERNAME/REPOSITORY.git (push)
3.2 Switching remote URLs from HTTPS to SSH
-
Change the current working directory to your local project.
-
List your existing remotes in order to get the name of the remote you want to change.
git remote -v origin https://github.com/USERNAME/REPOSITORY.git (fetch) origin https://github.com/USERNAME/REPOSITORY.git (push)
-
Change your remote's URL from HTTPS to SSH with the
git remote set-url
command.git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
-
Verify that the remote URL has changed.
git remote -v # Verify new remote URL origin git@github.com:USERNAME/REPOSITORY.git (fetch) origin git@github.com:USERNAME/REPOSITORY.git (push)
4.缓存密码Caching your GitHub password in Git
https://help.github.com/articles/caching-your-github-password-in-git/
If you're cloning GitHub repositories using HTTPS, you can use a credential helper to tell Git to remember your GitHub username and password every time it talks to GitHub.
If you clone GitHub repositories using SSH, then you authenticate using SSH keys instead of a username and password. For help setting up an SSH connection, see Generating an SSH Key.
Tip: You need Git 1.7.10 or newer to use the credential helper.
You can also install a native Git shell, such as Git for Windows. With Git for Windows, running the following in the command line will store your credentials:
git config --global credential.helper wincred
5.Auto-launching ssh-agent
on Git for Windows(缓存ssh rsa key于ssh-agent)
If you're using Git Shell that's installed with GitHub Desktop, you don't need to follow these steps. GitHub Desktop automatically launches ssh-agent
for you.
Otherwise, follow these steps to run ssh-agent
automatically when you open bash or Git shell. Copy the following lines and paste them into your ~/.profile
or ~/.bashrc
file in Git shell:(如果没有/users/yourname/下没有这两个文件,新建一个比如.profile文件,复制以下内容)
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add
fi
unset env
If your private key is not stored in one of the default locations (~/.ssh/id_rsa
or ~/.ssh/id_dsa
), you'll need to tell your SSH authentication agent where to find it. To add your key to ssh-agent, type ssh-add ~/path/to/my_key
. For more information, see "Generating a new SSH key and adding it to the ssh-agent"
Tip: If you want ssh-agent
to forget your key after some time, you can configure it to do so by running ssh-add -t <seconds>
.
Now, when you first run Git Bash, you are prompted for your passphrase:
Initializing new SSH agent...
succeeded
Enter passphrase for /c/Users/you/.ssh/id_rsa:
Identity added: /c/Users/you/.ssh/id_rsa (/c/Users/you/.ssh/id_rsa)
Welcome to Git (version 1.6.0.2-preview20080923)
>
Run 'git help git' to display the help index.
Run 'git help ' to display help for specific commands.
The ssh-agent
process will continue to run until you log out, shut down your computer, or kill the process.