Git 下载 、安装、规范

342 阅读3分钟

1、下载

官网地址:git-scm.com/download

2、安装

  1. 打开.exe安装程序, 点击next

  2. 选择安装路径(根据个人习惯选择)

  3. 选择组件

QQ20240827-092453.png

-   Additional Icons

    -   On the Desktop // 添加桌面图标

-   Windows Explorer integration // 右键菜单添加以下两个选项

    -   Git Bash Here
    -   Git GUI Here

-   Git LFS (Large File Support // 大文件支持

-   Associate .git* configuration files with the default text editor // 关联.git后缀文件

-   Associate .sh files to be run with Bash // 关联.sh文件

-   Check daily for Git for Windows updates // 每天检查版本更新

-   (NEW!) Add a Git Bash Profile to Windows Terminal // 将Git Bash添加到Windows Terminal中

-   (NEW!)Scalar (Git add-on to manage large-scale repositories) Windows新开发的一种大规模仓库管理, 视情况而定

4. 后续所有步骤无脑下一步

3、更新

1、在官网下载新版本,双击安装程序,窗口左下角勾选 Only show new options 选项,然后点击 Install

QQ20240827-092345.png

2、点击 Finish 完成更新

4、配置

1. 基础用户配置

# 设置用户名和邮箱(全局配置)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 设置用户名和邮箱(仅当前仓库)
git config user.name "Your Name"
git config user.email "your.email@example.com"

# 查看配置
git config --list
git config user.name  # 查看特定配置

# 查看配置文件位置
git config --list --show-origin

2. 文件系统配置

区分大小写设置
# 查看当前仓库的大小写敏感设置
git config core.ignorecase

# 设置为大小写敏感(推荐)
git config core.ignorecase false

# 设置为大小写不敏感(Windows默认)
git config core.ignorecase true

# 全局设置
git config --global core.ignorecase false

说明

  • false: 大小写敏感,推荐设置
  • true: 大小写不敏感,Windows系统默认
  • 建议设置为 false,避免文件名大小写问题
行尾符配置
# 自动转换行尾符(推荐)
git config --global core.autocrlf true  # Windows
git config --global core.autocrlf input # macOS/Linux

# 不转换行尾符
git config --global core.autocrlf false

# 检查行尾符
git config core.autocrlf

3. Pull 策略配置

设置默认pull策略
# 设置为merge策略(默认)
git config --global pull.rebase false

# 设置为rebase策略
git config --global pull.rebase true

# 设置为fast-forward only
git config --global pull.ff only

# 查看当前设置
git config pull.rebase

三种策略对比

策略命令特点适用场景
mergegit pull创建合并提交,保留分支历史团队协作,需要保留完整历史
rebasegit pull --rebase线性历史,更清晰个人开发,追求简洁历史
fast-forward onlygit pull --ff-only只允许快进合并严格的分支管理
推荐配置
# 个人开发推荐
git config --global pull.rebase true

# 团队协作推荐
git config --global pull.rebase false

4. 安全配置

# 设置凭证存储
git config --global credential.helper store  # 永久存储
git config --global credential.helper cache  # 临时存储(15分钟)

# 设置SSH密钥(推荐)
# 生成SSH密钥
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

# 添加到SSH代理
ssh-add ~/.ssh/id_rsa

5. 完整配置示例

# 基础配置
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "code --wait"

# 文件系统配置
git config --global core.ignorecase false
git config --global core.autocrlf input  # macOS/Linux

# Pull策略配置
git config --global pull.rebase true

# 别名配置
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'

# 安全配置
git config --global credential.helper store

5、提交规范

1、项目安装git工具

  • husky
  • commitizen

2、规范格式

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]
  • 类型(Type) :表示提交的类型,如feat(新功能)、fix(修复)、docs(文档更新)等。
  • 范围(Scope,可选) :指定提交影响的代码模块或功能区域。
  • 描述(Description) :简短描述提交的内容。
  • 正文(Body,可选) :提供更详细的描述,如变更的原因、影响范围等。
  • 脚注(Footer,可选) :提供额外信息,如关联的issue编号、破壊性变更的标记等。

例如:

git commit -m feat(login): Add login authorization feature

git commit -m docs:文档更新

3、具体属性

属性描述
feat新功能
fix修改bug
docs文档修改
style格式修改
refactor重构
perf性能提升
test测试
build构建系统
ci对CI配置文件修改
chore修改构建流程、或者增加依赖库、工具
revert回滚版本