跳到主要内容

常用技巧

Config 配置

Git 配置文件分为三级,系统级(--system)、用户级(--global)和目录级(--local),三者的使用优先级以离目录(repository)最近为原则,如果三者的配置不一样,则生效优先级 目录级>用户级>系统级

  1. 系统级(/etc/gitconfig):使用 --system 来进行配置,该配置对系统上所有用户及他们所拥有的仓库都生效的配置值。
git config --system user.name "用户名"
git config --system user.email "邮箱地址"`
  1. 用户级(~/.gitconfig):使用 --global 来进行配置,该配置对当前用户上所有的仓库有效
git config --global user.name "用户名"
git config --global user.email "邮箱地址"
  1. 目录级(.git/config):使用 --local 来进行配置,只对当前仓库生效。
git config --local user.name "用户名"
git config --local user.email "邮箱地址"

设置默认分支

可以根据需求设置为系统级(--system)、用户级(--global)和目录级(--local)

git config --global init.defaultBranch <分支名称>

多平台换行符问题(LF or CRLF)

文本文件所使用的换行符,在不同的系统平台上是不一样的。UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是 0x0D(CR),后来的 OS X 在更换内核后与 UNIX 保持一致了。但 DOS/Windows 一直使用 0x0D0A(CRLF) 作为换行符。

# 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true
# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input
# 提交检出均不转换
git config --global core.autocrlf false
# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true
# 允许提交包含混合换行符的文件
git config --global core.safecrlf false
# 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

如果涉及到在多个系统平台上工作,推荐将 git 做如下配置:

git config --global core.autocrlf input
git config --global core.safecrlf true

Alias 重命名别名

git config --global alias.st status //status 缩写成 st
git config --global alias.co checkout //checkout 缩写成 co
git config --global alias.br branch //branch 缩写成 br
git config --global alias.ci commit //commit 缩写成 ci
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias g='git'
alias ga='git add'
alias gco='git checkout'
alias gcb='git checkout -b'
alias gcm='git checkout master'
alias gcd='git checkout develop'
alias gd='git diff'
alias gf='git fetch'
alias gfo='git fetch origin'
alias gl='git pull'
alias gp='git push'

Git 常用命令,可以根据实际需要创建缩写命令。

GPG 签名提交

  1. 生成 GPG 密钥对
// 1.1 创建 GPG 密钥
gpg --full-gen-key

// 1.2 列出 GPG 密钥
gpg --list-secret-keys --keyid-format LONG "your_email"

// 1.3 复制 GPG 密钥 ID,以下示例复制 4AEA00A342C24CA3。
sec ed25519/4AEA00A342C24CA3 2021-09-14 [SC]
6DE3507E82DEB6E8828FAAC34AEA00A342C24BD4
uid [ 绝对 ] your_name "your_email"
ssb cv25519/812B586FD245B560 2021-09-14 [E]

// 1.4 导出 GPG 公钥(以上述 ID 为例)
gpg --armor --export 4AEA00A342C24CA3
  1. 平台添加 GPG 公钥配置

将 GPG 公钥添加到 git 托管平台,例如 Github、Gitlab、Gitee 等。

  1. 本地 Git 仓库关联 GPG 密钥
// 3.1 列出 GPG 密钥
gpg --list-secret-keys --keyid-format LONG "your_email"

// 3.2 复制 GPG 密钥 ID,以下示例复制 4AEA00A342C24CA3。
sec ed25519/4AEA00A342C24CA3 2021-09-14 [SC]
6DE3507E82DEB6E8828FAAC34AEA00A342C24BD4
uid [ 绝对 ] your_name "your_email"
ssb cv25519/812B586FD245B560 2021-09-14 [E]

// 3.3 本地 Git 仓库中配置该密钥,对 commit 提交进行签名。
git config --global user.signingkey 4AEA00A342C24CA3
  1. 签名 Git commit

运行 Git commit 命令时需要用到 -S 参数,如果不希望每次都要输入 -S 标志,可以配置默认签名。

// 签名提交
git commit -S -m "your_commit_message"

// 开启默认签名提交
git config --global commit.gpgsign true
  1. 验证签名

最后可以推送签名提交到 git 托管平台,验证是否签名成功!

压缩提交记录

如果在新分支进行了多次提交,然后想合并到主分支。可以使用 --squash 选项,压缩合并提交。

// 将目标分支的提交压缩为一个,然后合并分支
git merge --squash <branchName>

忽略文件更改

  • 未提交:添加忽略文件相对路径到 .gitignore 中即可。

  • 已提交:git rm --cached file,添加忽略文件相对路径到 .gitignore 中即可。