跳转至

Git 配置

0. 简介

本文介绍 Git 的基础配置。

对于符号链接,Windows 需要额外设置才能获得更好的兼容性。

这里重点关注 Git 在 Windows 与 Unix-like 系统之间的兼容性。

1. Git 初始化

  • 配置用户名
git config --global user.name 'NAME'
  • 配置用户邮箱
git config --global user.email 'EMAIL@EXAMPLE.COM'

2. 使用 GPG 配置 Git

  • 配置 signing key
git config --global user.signingkey XXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXX:个人 GPG 私钥的 LONG 或 SHORT 格式。

  • 配置 GPG 程序
git config --global gpg.program C:\\Program Files (x86)\\GnuPG\\bin\\gpg.exe
  • 配置 Commit 和 Tag 签名
git config --global commit.gpgsign true
git config --global tag.gpgsign true

3. 在 Windows 上配置 Git,以兼容 Unix-like 系统

  • 配置 filemode 权限
git config --global core.filemode false
  • 配置 CRLF 和 LF 自动转换
git config --global core.autocrlf true
  • 配置大小写敏感
git config --global core.ignorecase false
  • 配置符号链接
git config --global core.symlinks true

3.1 Windows 上的额外设置

  • 在 Windows 10 及以上版本中启用开发者模式。
  • 安装 Git for Windows 时启用 symlinks
  • 组策略:
  • 进入创建符号链接权限设置:
  • Computer Configuration → Windows Settings → Security Settings → Local Policies → User Rights Assignment → Create symbolic links
  • 输入用户名,点击 “Check Names”,然后确认。

4. 默认忽略 Git LFS 拉取

git config --global filter.lfs.smudge "git-lfs smudge --skip -- %f"
git config --global filter.lfs.process "git-lfs filter-process --skip"

5. .gitconfig 示例

[core]
      symlinks = true
      autocrlf = true
      filemode = false
      ignorecase = false
[user]
    name = User Name
    email = Username@example.com
    signingkey = XXXXXXXXXXXXXXXX
[commit]
    gpgsign = true
[tag]
    gpgsign = true
[gpg]
    program = C:\\Program Files (x86)\\GnuPG\\bin\\gpg.exe
[filter "lfs"]
    smudge = git-lfs smudge --skip -- %f
    process = git-lfs filter-process --skip

6. 在 Windows 上创建符号链接

mklink [[/d] | [/h] | [/j]] <link> <target>

/d Creates a directory symbolic link. By default, this command creates a file symbolic link.

/h Creates a hard link instead of a symbolic link.

/j Creates a Directory Junction.

<link> Specifies the name of the symbolic link being created.

<target> Specifies the path (relative or absolute) that the new symbolic link refers to.

/? Displays help at the command prompt.

7. 添加 Tag

git tag XXXX

8. 删除 Tag

git tag -d <tagname>
git push origin --delete <tagname>

9. 推送 Tag

git push origin <tagname>

10. 删除 Git 分支

git branch -d <branchname>
git push origin --delete <branchname>

REF

[1]. https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work

[2]. https://github.com/git-for-windows/git/wiki/Symbolic-Links

[3]. https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mklink

[4]. 从一个git仓库提交代码到另一个仓库

[5]. GitLab-CI-CD-Push-to-Repository