Git 使用指南

Git 基础配置

1. 全局配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 配置用户信息
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 init.defaultBranch main

# 配置行尾处理
git config --global core.autocrlf false 
# 说明:
# - true:提交时转换为LF,检出时转换为CRLF(Windows推荐)
# - false:不进行自动转换(跨平台开发推荐)
# - input:提交时转换为LF,检出时不转换(Linux/Mac推荐)

# 配置中文显示
git config --global core.quotepath false

2. 常用别名配置

1
2
3
4
5
# 配置常用命令别名
git config --global alias.st status
git config --global alias.br branch
git config --global alias.ci commit
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"

Git 新特性

Git 2.23 及以后版本

版本新特性说明
2.23git switch简化分支切换操作,替代部分 git checkout 功能
2.23git restore恢复文件状态的新命令,替代部分 git checkoutgit reset 功能
2.24git sparse-checkout允许只检出仓库部分内容,节省空间
2.25git switch/restore 增强改进功能和稳定性
2.26git restore --source允许指定恢复内容的源
2.27git commit --verbose提交时显示详细变更信息
2.28git config includeIf支持条件性包含配置文件
2.29git diff --inter-hunk-context增强 diff 上下文显示
2.30git commit --no-verify跳过钩子验证的提交选项
2.31git switch --orphan创建新的孤立分支
2.32git commit --dry-run预查看提交内容
2.33git branch --show-current显示当前分支的简化命令
2.34git rebase --exec在变基操作后执行指定命令

常用操作

1. 仓库初始化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 初始化新仓库
git init

# 克隆远程仓库
git clone <repository_url>

# 克隆特定分支
git clone -b <branch_name> <repository_url>

# 浅克隆(只获取最新版本)
git clone --depth 1 <repository_url>

2. 分支操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 创建并切换分支
git switch -c <branch_name>

# 切换分支
git switch <branch_name>

# 切换到上一个分支
git switch -

# 创建新的孤立分支(不包含历史提交)
git switch --orphan <branch_name>

# 查看分支
git branch

# 删除分支
git branch -d <branch_name>

# 强制删除分支
git branch -D <branch_name>

3. 文件恢复

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 恢复工作区文件(丢弃未暂存的修改)
git restore <file_name>

# 恢复多个文件
git restore <file1> <file2>

# 恢复所有未暂存的修改
git restore .

# 从暂存区取消暂存(但保留工作区的修改)
git restore --staged <file_name>

# 从指定提交或分支恢复文件
git restore --source=HEAD~1 <file_name>  # 从上一次提交恢复
git restore --source=main <file_name>    # 从main分支恢复

# 恢复文件到特定版本
git restore --source=<commit_hash> <file_name>

# 注意:如果文件同时存在于工作区和暂存区的修改
# --staged 和 --worktree 可以组合使用
git restore --staged --worktree <file_name>  # 同时丢弃暂存区和工作区的修改

4. 提交操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 暂存文件
git add <file_name>

# 暂存所有更改
git add .

# 提交更改
git commit -m "commit message"

# 修改最后一次提交
git commit --amend

5. 远程操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 添加远程仓库
git remote add <remote_name> <repository_url>

# 推送到远程
git push <remote_name> <branch_name>

# 拉取更新
git pull <remote_name> <branch_name>

# 强制推送(谨慎使用)
git push -f <remote_name> <branch_name>

高级操作

1. 覆盖所有提交

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 创建孤立分支
git checkout --orphan temp_branch

# 添加所有文件
git add .

# 提交更改
git commit -m "init"

# 删除主分支
git branch -D main

# 重命名分支
git branch -m temp_branch main

# 强制推送到远程
git push -f origin main

注意:此操作不可撤销,请谨慎使用!

2. 交互式变基

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 开始交互式变基
git rebase -i HEAD~3  # 修改最近3次提交

# 常用变基命令
# p, pick = 使用提交
# r, reword = 使用提交,但修改提交信息
# e, edit = 使用提交,但停止修改
# s, squash = 使用提交,但合并到前一个提交
# f, fixup = 类似 squash,但丢弃提交信息
# d, drop = 删除提交

3. 暂存操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 暂存当前更改
git stash

# 暂存并添加描述
git stash push -m "stash message"

# 查看暂存列表
git stash list

# 应用暂存
git stash pop  # 应用并删除
git stash apply  # 应用但保留

# 删除暂存
git stash drop <stash_id>

常见问题

1. 中文显示问题

1
2
# 解决中文文件名显示问题
git config --global core.quotepath false

2. 大文件处理

1
2
3
4
# 使用 Git LFS 管理大文件
git lfs install
git lfs track "*.psd"
git lfs track "*.zip"

3. 凭证管理

1
2
3
# 配置凭证缓存
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'

最佳实践

  1. 提交规范

    • 使用清晰的提交信息
    • 遵循约定式提交规范
    • 保持提交粒度适中
  2. 分支管理

    • 使用功能分支开发
    • 定期合并主分支
    • 及时删除无用分支
  3. 代码审查

    • 提交前自测
    • 使用 Pull Request
    • 进行代码审查
  4. 安全实践

    • 定期备份仓库
    • 谨慎使用强制推送
    • 保护敏感信息

57.12k 字
43篇文章