关于git操作

查看本地是否已有SSH

  1. 在本地打开 git bash 命令行窗口,输入以下命令
    cd ~/.ssh

  2. 如果没有,显示
    bash: cd: /c/Users/Administrator/.ssh: No such file or directory

  3. 否则表示本地以创建 SSH key 了

    1
    2
    3
    cd ~/.ssh
    ls
    id_rsa id_rsa.pub known_hosts

创建 SSH key

  1. 最后一个参数替换为自己的 Gitee || Github 注册邮箱

    1
    2
    ssh-keygen -t rsa -C "your_email@example.com"
    Generating public/private rsa key pair.
    1
    ssh-keygen -t rsa -C "your_email@example.com" -b 4096 -f ~/.ssh/custom_key
  2. 接下来会提示输入生成的key存放的路径,不设置直接回车的话会默认创建在C:/Users/你的用户账号/.ssh文件夹下

    1
    2
    Enter file in which to save the key (/c/Users/Him/.ssh/id_rsa):
    Created directory '/c/Users/userpath/.ssh'.
  3. 再接下来会提示你输入密码,这个密码是用来每次提交的时候输入确认,可以不设置,直接回车两次

    1
    2
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
  4. 最后成功后会看到类似如下的输出,表示成功生成SSH key了,可以到C:/Users/你的用户账号/.ssh文件夹下查看

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    Your identification has been saved in /c/Users/Him/.ssh/id_rsa.
    Your public key has been saved in /c/Users/Him/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:RwvBINgH8CEt2KniltmykeyDsOseUYcwMzehFeyT86s xxxxxx@qq.com
    The key's randomart image is:
    +---[RSA 2048]----+
    | o+%OO+o. |
    |..=+%*+ .. |
    | ..+o+o.. . |
    |o. o=. o . |
    |o oolalala S o |
    | +.+.. . . |
    |. .o . |
    | . . . |
    | . E. |
    +----[SHA256]-----+

密钥

RSA 4096位密钥(推荐兼容性方案)

1
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -C "your_email@example.com"

Ed25519密钥(推荐安全方案)

1
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C "your_email@example.com"

ECDSA 521位密钥(平衡方案)

1
ssh-keygen -t ecdsa -b 521 -f ~/.ssh/id_ecdsa -C "your_email@example.com"

带密码密钥

带密码保护的密钥(示例)

1
ssh-keygen -t ed25519 -f ~/.ssh/secure_key -N "your_strong_password"
  • -t:指定密钥类型(rsa/ed25519/ecdsa)
  • -b:定义密钥长度(RSA/ECDSA需指定)
  • -f:自定义密钥文件路径
  • -C:添加注释(通常为邮箱)
  • -N:设置密钥密码(空表示无密码)

SSH验证

  1. 验证本地生成的ssh,github或者gitee端密钥是否添加成功,输入如下命令

    1
    2
    ssh -T git@github.com
    ssh -T git@gitee.com
  2. 如果添加成功就会显示如下语句

    Hi Username! You’ve successfully authenticated, but GitHub does not provide shell access.
    Hi Username! You’ve successfully authenticated, but GITEE.COM does not provide shell access.

  3. Username:是你的在git端的名称


同一台电脑上多个github或gitee账户

同一台电脑有两个github账号或者两个gitee账号,配置如下都相同

  1. 请参考:gitee文档 配置文件如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # gitee xxxxxx@qq.com 
    Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/giteeqq_id_rsa

    # gitee xxxxxxx@189.cn
    Host xxxx.gitee.com #此处xxxx需要修改为你的gitee名称
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitee189_id_rsa

    # github aaaaa@189.cn
    Host aaaaa.github.com #此处aaaaa需要修改为你的github名称
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github189_id_rsa

    # github xxxx@qq.com
    #Host xxxx.github.com #此处xxxx需要修改为你的github名称
    #HostName github.com
    #PreferredAuthentications publickey
    #IdentityFile ~/.ssh/id_rsa
  2. 文件中有多个xxxaaaaa都是需要修改处,如果需要验证添加SSH是否成功,测试代码如下,对应上面配置文件
    1
    2
    3
    4
    ssh -T git@gitee.com
    ssh -T git@xxxx.gitee.com
    ssh -T git@aaaaa.github.com
    ssh -T git@xxxx.github.com
    依此类推有多少个gitee或github就配置多少个,特别注意 xxxx_id_rsa 不能配置为相同文件名。

git 问题

  1. GitHub Desktop 使用ssh推送或者下拉时候报错 Authentication failed. Some common reasons include:

解决方法:

可以尝试关闭路径 File->Options->Advanced->SSH的选项->Use system OpenSSH 选项

  1. fatal: unable to access ‘git/‘: Send failure:Connection was reset

解决方法:

为了将来参考,我通过重新打包解决了这个问题:

git repack --max-pack-size=100M -a -d

git指令

git remote 指令

git remote 命令用于用于管理 Git 仓库中的远程仓库。

git remote 命令提供了一些用于查看、添加、重命名和删除远程仓库的功能。

1
2
3
4
5
6
7
8
9
10
11
12
git remote   列出当前仓库中已配置的远程仓库。
git remote -v 列出当前仓库中已配置的远程仓库,并显示它们的 URL。

添加一个新的远程仓库。指定一个远程仓库的名称(origin)和 URL(https://github.com/user/repo.git),将其添加到当前仓库中。
git remote add origin https://github.com/user/repo.git

git remote rename origin neworigin 将已配置的远程仓库重命名。 origin 重命名 neworigin
git remote remove deleteorigin 从当前仓库中删除指定的远程仓库。删除 `deleteorigin`

修改指定远程仓库的 URL。将 origin 仓库地址 改成 https://github.com/user/origintest.git
git remote set-url origin https://github.com/user/new-repo.git
git remote show origin 显示指定远程仓库的详细信息,包括 URL 和跟踪分支。

使用方法:

查看当前的远程仓库URL

1
git remote -v

添加远程仓库URL

1
git remote add master utl(https://xxxxx.git)