Git从入门到精通

Git 不完全教程

Git安装

Windows

官网:https://git-scm.com/download/win

Linux

官网:https://git-scm.com/download/linux

Mac

官网:https://git-scm.com/download/mac

连接使用

  1. 打开git bash,使用命令生成ssh公钥和私钥

    ssh-keygen -t rsa -C '[email protected]',一路回车

  2. git bash使用命令查看生成的密钥

    cat ./.ssh/id_rsa.pub,复制输出的内容到相应Git系统

    GitLabimage-20230420211432397

    Github

    image-20230420211611675

添加多个Git密钥

  1. 生成一对密钥ssh key,for GitLab

    1
    ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab-rsa
  2. 生成一对密钥ssh key,for Github

    1
    ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/github-rsa
  3. ./ssh文件夹下新建文件名为config,用于管理多个ssh key

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    # 两个Github
    Host github-user1
    HostName github.com
    User git
    IdentityFile ~/.ssh/my_new_key

    Host github-user2
    HostName github.com
    User git
    IdentityFile ~/.ssh/my_other_key

    # GitLab加Github
    Host gitlab-user1
    HostName gitlab.example.com
    User git
    IdentityFile ~/.ssh/my_new_key

    Host github-user2
    HostName github.com
    User git
    IdentityFile ~/.ssh/my_other_key

    # ssh-keygen -t rsa -b 4096 -C "my_new_key" -f ~/.ssh/my_new_key
    # HostName github.com表示要连接到远程主机的名称或IP地址。在这个例子中,它们都连接到GitHub的主机。
    # User git将SSH连接的用户名设置为Git,这是GitHub默认的SSH用户名。
    # IdentityFile选择项指定了私钥文件的路径,为每个用户的SSH连接分配了不同的私钥文件。在这个例子中,第一个用户的私钥文件的路径为,第二步路径~/.ssh/my_new_key为~/.ssh/my_other_key。

    当使用不同git用户时,需要你更改一部分输入对应config内容。

    1
    git clone git@github-user1:user/repo.git

参考文献:

  1. [图解