Git 基础操作手册:从入门到进阶

1. Git 数据流转模型

理解 Git 的核心在于掌握数据在不同区域间的流转。

%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#4F46E5', 'primaryTextColor': '#fff', 'primaryBorderColor': '#3730A3', 'lineColor': '#6366F1', 'secondaryColor': '#10B981', 'tertiaryColor': '#F59E0B'}}}%%
flowchart LR
    Workspace["工作区<br/>(Workspace)"] -->|"git add"| Index["暂存区<br/>(Index/Stage)"]
    Index -->|"git commit"| Repository["本地仓库<br/>(Local Repo)"]
    Repository -->|"git push"| Remote["远程仓库<br/>(Remote Repo)"]
    Remote -->|"git pull/fetch"| Repository
    Repository -->|"git checkout/restore"| Workspace
    
    classDef primary fill:#4F46E5,stroke:#3730A3,color:#fff
    class Workspace,Index,Repository,Remote primary

2. 基础操作

2.1 常用核心命令

  1. 添加文件:git add <file> 将工作区修改提交到暂存区。

  2. 提交版本:git commit -m "message" 将暂存区内容持久化到本地仓库。

  3. 拉取远程:git pull 获取并合并远程仓库的更新。

  4. 推送远程:git push 将本地提交推送到远程服务器。

  5. 差异查看:

    1
    2
    3
    4
    # 查看两个提交版本间的差异
    git diff <hash1> <hash2>
    # 仅查看受影响的文件列表
    git diff <hash1> <hash2> --name-only

2.2 日志查看技巧

除标准 git log 外,推荐配置图形化别名以清晰查看分支演变:

1
2
3
4
5
# 配置别名 git lg
git config --global alias.lg "log --color --graph --pretty=format:'%C(yellow)%h%Creset -%C(red)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date=format:'%Y-%m-%d %H:%M'"


git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"

3. 标签与版本管理

3.1 标签 (Tag) 操作

标签常用于发布里程碑版本。

  • 新建标签:git tag <name>(默认为当前 HEAD)或 git tag <name> <commit_id>
  • 带备注标签:git tag -a <name> -m "version description"
  • 查看标签:git tag 列出所有本地标签。
  • 推送标签:
    • 推送指定标签:git push origin <tagname>
    • 推送所有未推送标签:git push origin --tags
  • 删除标签:
    • 本地删除:git tag -d <tagname>
    • 远程删除:git push origin :refs/tags/<tagname>

3.2 交互式变基 (Rebase)

用于合并多次提交记录,保持提交历史整洁:

1
2
3
4
5
6
# 合并当前分支最近的 N 次提交
git rebase -i HEAD~N

# 操作指令说明:
# pick: 保留该 commit
# squash: 将该 commit 合并入前一个提交

4. 分支管理

分支是 Git 协作的核心。

4.1 常用分支操作

  • 查看分支:git branch(本地)或 git branch -a(所有)。
  • 创建与切换:
    • 创建分支:git branch <name>
    • 切换分支:git checkout <name> 或现代命令 git switch <name>
    • 创建并切换:git checkout -b <name>git switch -c <name>
  • 重命名分支:git branch -m <old_name> <new_name>
  • 合并分支:git merge <name> 将指定分支合并到当前分支。
  • 删除分支:git branch -d <name>

4.2 批量操作实战

在大型项目中,经常需要清理过期分支:

1
2
3
4
5
# 批量删除匹配 'feature' 关键词的远程分支(排除特定分支)
git fetch && git branch -r | grep origin/feature | grep -v "important-branch" | sed 's/origin\///' | xargs -I {} git push origin --delete {}

# 批量删除本地匹配的分支
git branch | grep feature | grep -v "important-branch" | xargs -I {} git branch -d {}

5. 回滚与撤销

回滚操作需谨慎,建议根据修改所处的阶段选择对应的策略。

5.1 撤销未提交的修改 (Workspace)

1
2
3
4
# 丢弃工作区的修改,恢复到最后一次提交的状态
git checkout -- <file>
# 或使用现代命令
git restore <file>

5.2 撤销已暂存的修改 (Index)

1
2
3
4
# 将文件从暂存区移回工作区(保留修改)
git reset HEAD <file>
# 或使用现代命令
git restore --staged <file>

5.3 撤销已提交的记录 (Repository)

  • 修正最后一次提交:git commit --amend -m "new message"

  • 回滚版本:

    1
    2
    3
    4
    # 强制回退到指定版本(丢弃该版本后的所有修改,慎用!)
    git reset --hard <hash>
    # 软回退(回退版本号,保留工作区代码以便重新提交)
    git reset --soft <hash>
  • 记录全指令历史:若误操作回滚,使用 git reflog 查找丢失的提交哈希。

5.4 特殊场景:停止追踪文件

如果文件已入库但应被忽略(如日志文件):

1
2
# 从索引中移除但不删除物理文件
git rm --cached <file>

6. 环境配置

6.1 基础用户信息

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

6.2 进阶配置

  • 显示中文:解决 git status 中文路径乱码。

    1
    git config --global core.quotepath false
  • 区分大小写:Git 默认对文件名大小写不敏感,建议关闭此特性。

    1
    git config --global core.ignorecase false
  • 代理设置

    1
    2
    3
    4
    # 设置代理
    git config --global http.proxy 'http://127.0.0.1:8123'
    # 取消代理
    git config --global --unset http.proxy

6.3 全局忽略文件

创建并配置全局忽略列表(如 .DS_Store, .idea):

1
2
3
# 在 ~/.gitconfig 中配置
[core]
excludesfile = ~/.config/git/ignore