10

关于 Git 同一终端多用户及 22 端口断连的配置

 2 years ago
source link: https://blog.ichr.me/post/git-more-user-and-solve-github-22-port-disconnected/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Git 多用户

笔者有多个 GitHub 账号,一个是放出来贴一些东西的,另一个才是自己平时使用的账号。但是在同一台计算机下操控两个账号总是会有些不方便,我们要特殊配置一下。

Global Config

如果以前为了方便设置了全部变量,如 user.name, user.email 等。在使用两个账号的时候就需要取消掉这些配置:

  1. 查看设置了哪些全局变量:

    git config --global --list
  2. 然后将其逐个清除:

    git config --global --unset user.name
    git config --global --unset user.email
    ...
  3. 然后在单个文件夹下添加相应配置:

    git config --local user.name "your_name" && git config --local user.email "your_email"

P.S. 其实不清除全局配置也是可以的,默认优先级是:local > global > system ,不用全局配置的 Package 记得设置一下 local 参数。

SSH 配对

之前生成过一个账号的 SSH 钥对,现在添加新的。

  1. 本地生成新的 SSH 密匙:

    ssh-keygen -t rsa -C "your_email"
  2. 在接下来的配置中不要使用默认,不然可能将之前的覆盖掉,例如换成:

    Enter file in which to save the key (/Users/user/.ssh/id_rsa): id_rsa_conpany
  3. 使用 Config 文件对这两个密匙分别对应:

    cd {your ssh file}  # eg.(for Mac) /Users/user/.ssh
    
    touch config  # 新建 config 文件
    
    vim config  # 修改 config 配置
  4. 将 config 配置设置为类似:

    # Personal
    Host personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa
    
    # Company
    Host company
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_company
  5. 然后要重新配置以前使用 ssh 连接的包

    git remote rm origin
    git remote add origin git@personal:xxx/xxx.git
  6. 之后尝试一下是否可以正常 git push 即可

22 端口连接超时

由于堪忧的国际出入口带宽性能,最近更新的时候发现本地一直连不上 GitHub ,显示:

ssh: connect to host github.com port 22: Operation timed out

这造成了许多麻烦,所以我这里记录一个解决方案。

  1. 修改 ssh 目录下的 config 文件,如果没有,就创建一个:

    cd {your ssh file}  # eg.(for Mac) /Users/user/.ssh
    
    touch config  # 新建 config 文件
    
    vim config  # 修改 config 配置
  2. 默认 config 配置,如果你进行了上面 Git 多用户的修改,就只需要改 HostName, Post

    Host github.com
    HostName ssh.github.com
    User git
    IdentityFile ~/.ssh/id_rsa
    Port 443
  3. 保存应用即可。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK