Git 笔记

git 简介

本文介绍 git 的配置及使用

git 配置

配置文件位置

Git 相关的配置文件有三个:

  1. 系统级
    用户目录中的 .gitconfig 文件
    1
    ~/.gitconfig
  2. 系统用户级
    用户目录中的 .gitconfig 文件
    1
    ~/.gitconfig
  3. 仓库级
    git 项目目录中的 .git/config 文件
    1
    /项目目录/.git/config

对于同一配置项, 三个配置文件的优先级是 系统级 < 系统用户级 < 仓库级

全局设置

设置的是系统用户配置

1
2
git config --global user.name "your_name"
git config --global user.email "your_email@example.com"

单个仓库设置

1
2
git config user.name "your_name"
git config user.email "your_email@example.com"

查看 git 配置

查看当前配置, 在当前项目下面查看的配置是全局配置+当前项目的配置, 使用的时候会优先使用当前项目的配置

1
git config --list

添加 git ssh-key

生成 ssh-key

1
2
3
4
5
6
7
8
# 推荐 ed25519 算法
ssh-keygen -t ed25519 -C "your_email@example.com"
# 不支持 ed25519 算法的话用 rsa 算法
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 提示以下内容时: (按回车是默认)
>> Enter a file in which to save the key (/c/Users/YOU/.ssh/id_ALGORITHM):[Press enter]
>> Enter passphrase (empty for no passphrase):
>> Enter same passphrase again:

远端仓库添加 ssh-key

把 id_ALGORITHM.pub 里面的内容复制添加到远端仓库, 如: github 个人账号中

配置多个 ssh-key

  1. /c/Users/YOU/.ssh/ 文件夹中新建文件 config
  2. 配置 config 文件

config 文件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_ed25519

# gitlab
Host gitlab.com
HostName gitlab.*.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_ed25519

# 配置文件参数
# Host 标识了一个配置区段
# HostName 指定远程主机名, 可以直接使用数字IP地址
# Port 指定远程主机端口号, 默认为 22
# User 登录名
# IdentityFile 指定密钥认证使用的私钥文件路径, 默认为 ~/.ssh/id_ALGORITHM (必须全路径)
# UserKnownHostsFile 指定一个或多个用户认证主机缓存文件, 用来缓存通过认证的远程主机的密钥, 多个文件用空格分隔。默认缓存文件为: ~/.ssh/known_hosts, ~/.ssh/known_hosts2
# ProxyCommand 后面是代理命令

验证是否能连上 git server

用 github 示例:

1
2
3
4
# 用以下命令即可
ssh -T git@github.com
# 失败时加上 -v 可以查看详细log
ssh -T -v git@github.com

known_hosts 验证失败

当遇到 known_hosts 验证失败导致连接不上 git 仓库时会包含以下提示:

1
2
3
4
Add correct host key in ${user_dir}/.ssh/known_hosts to get rid of this message.
Offending RSA key in ${user_dir}/.ssh/known_hosts:1
RSA host key for ${git仓库} has changed and you have requested strict checking.
Host key verification failed.

解决办法:

以连不上 github.com 示例,执行命令: ssh-keygen -R github.com ,然后根据提示操作即可

配置代理

配置 /c/Users/YOU/.ssh/config

config 文件配置

1
2
3
4
5
6
7
8
9
10
11
12
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_ed25519
# Mac 走 socks5 代理
# ProxyCommand nc -v -x 127.0.0.1:1080 %h %p
# Windows 走 socks5 代理 (需要把 git安装目录/mingw64/bin 目录添加到环境变量)
ProxyCommand connect -S 127.0.0.1:1080 %h %p

# 配置文件参数
# ProxyCommand 后面是代理命令

git 使用

命令

tag

查看 tag

查看所有 tag

1
2
3
4
# 完整显示
git show-ref --tags
# 只显示 tag 名字
git tag -l

查看指定 tag

1
2
3
# 查看 1.1.x 的 tag 列表
git show-ref --tag | awk '$2~/1\.1\../ {print ":" $2}'
git tag -l | awk '$1~/1\.1\../ {print $1}'

语法解释:

  1. $2 对第二列进行操作, 即 refs/tags/x.x.x 这一列
  2. ~ 匹配
  3. '//' awk 模式匹配的开始结束标志
  4. 1\.1\.. 是正则匹配规则, 第一位是 1, 在正则中 . 匹配任意单个字符, 所以这里 \. 类似于转义, 纯粹标识字符 (.), 后面 的 \. 也是标识字符。 所以这个正则的含义解读为: 匹配第二列, 然后对第二列进行正则匹配 字符(1.1.x), x 表示任意单个字符。
  5. {print ":" $2} 输出字符串 :+第二列, 组成字符串 类似于 (:refs/tags/1.1.0)

拉取远程 tag

1
2
# 从远程拉取所有信息
git fetch origin -t

删除 tag

  • 单个删除 tag
1
2
3
4
# 删除本地 tag
git tag -d <tagname>
# 删除远程 tag
git push origin :refs/tags/<tagName>
  • 批量删除指定 tag
1
2
3
4
5
# 删除远程 1.1.x 的 tag 列表
git show-ref --tag | awk '$2~/1\.1\../ {print ":" $2}' | xargs git push origin
# 删除本地 1.1.x 的 tag 列表
git tag -l | awk '$1~/1\.1\../ {print $1}' | xargs git tag -d
或 git tag -l | awk '/1\.1\../' | xargs git tag -d

语法解释:

  1. xargs git push origin 将前面的输出结果作为参数执行删除命令
  • 批量所有 tag
1
2
3
4
# 删除远程所有 tag 列表
git show-ref --tag | awk '{print ":" $2}' | xargs git push origin
# 删除本地所有 tag 列表
git tag -l | xargs git tag -d
  • 删除所有本地 tag, 并从远端重新拉取
1
2
3
# 执行下面两个命令
git tag -l | xargs git tag -d
git fetch origin -t

参考资料

  1. macOS 给 Git(Github) 设置代理(HTTP/SSH)