Skip to content

Git

下载 & 安装

  1. 下载 Git
  2. 部分安装选项说明:
    • Add a Git Bash Profile to Windows Terminal

      勾选的话,会将 Git Bash 添加到 Windows Terminal 中。

    • Windows Explorer Internal

      如果已经集成到 Windows Terminal 中,可以不勾选 Git BashGit GUI,右键菜单更简洁。

git 配置

配置文件路径:~/.gitconfig

查看配置:

bash
git config --global --list      # 查看所有
git config --global user.name   # 查看某个(如:用户名)

其他常用配置:

bash
# 忽略大小写,默认值 true
git config --global core.ignoreCase false

# 忽略文件权限变化,默认值 true
git config --global core.filemode false

# eol
git config --global core.autocrlf false
git config --global core.eol lf

基本配置

bash
git config --global user.name "example"
git config --global user.email "example@mail.com"

SSH

ssh-keygen

  • -t:指定密钥类型。
  • -C:添加注释。
  • -f:(可选)指定密钥文件的保存路径和名称。

配置 SSH

  1. 生成 SSH Key:

    bash
    ssh-keygen -t ed25519 -C "key_name"
    
    # 兼容旧版本
    # ssh-keygen -t rsa -b 4096 -C "key_name"
  2. 紧接之后三次回车。

  3. 成功执行后,会生成 ~/.ssh 文件夹,包含公钥 id_*.pub、私钥 id_*,例如:

    bash
    ├── id_rsa     # 私钥
    └── id_rsa.pub # 公钥
  4. 将公钥内容复制到 GitHub 中。

    操作路径:Settings -> SSH and GPG keys -> New SSH key

  5. 克隆测试

    配置好 ssh 后,以后克隆仓库使用 ssh 地址,例如:

    bash
    git clone git@github.com:example/repo.git

SSH 代理

  1. 安装 corkscrew

    bash
    sudo apt update
    sudo apt install -y corkscrew
  2. 编辑 ~/.ssh/config(示例:让 git@github.com 默认走代理)

    bash
    Host github.com
      HostName ssh.github.com
      Port 443
      User git
      IdentityFile ~/.ssh/id_ed25519
      IdentitiesOnly yes
      ProxyCommand corkscrew 192.168.1.8 7897 %h %p
  3. 连接测试

    bash
    ssh -T git@github.com

    如果返回如下结果说明成功:

    bash
    Hi VfanLee! You've successfully authenticated, but GitHub does not provide shell access.

配置多个 SSH Key

  1. 生成多个 SSH Key:

    bash
    # 例如:个人
    ssh-keygen -t ed25519 -C "key_name" -f ~/.ssh/id_ed25519_personal
    
    # 例如:工作
    ssh-keygen -t ed25519 -C "key_name" -f ~/.ssh/id_ed25519_work
  2. 配置 ~/.ssh/config

    bash
    Host github.com
      HostName ssh.github.com
      Port 443
      User git
      IdentityFile ~/.ssh/id_ed25519
      IdentitiesOnly yes
      ProxyCommand corkscrew 192.168.1.8 7897 %h %p
    
    Host codeup.aliyun.com
      HostName codeup.aliyun.com
      Port 22
      User git
      IdentityFile ~/.ssh/id_ed25519_work
      IdentitiesOnly yes
  3. 由于确保每个 git 账号用户名和邮箱不同,需要额外配置 git:

    • 【方法一】在每个仓库单独配置:

      bash
      git config user.name "your-name"
      git config user.email "your-eexample@mail.com"
    • 【方法二】通过 ~/.gitconfig 配置:

      bash
      [user]
        name = example
        email = example@mail.com
      
      [includeIf "gitdir:~/work/**"]
        path = ~/.gitconfig-work
      bash
      [user]
        name = user-name-work
        email = user-email@work.com

HTTP 代理

提示

可直接使用 bash 的环境变量,而无需配置 git。

设置代理

bash
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy https://127.0.0.1:7897

取消代理

bash
git config --global --unset http.proxy
git config --global --unset https.proxy

基于 MIT 许可发布