Git 版本控制

分布式版本控制系统


第一章 Git基础

1.1 Git简介

Git是一个开源的分布式版本控制系统,由Linus Torvalds于2005年创建。

特点
- 分布式
- 快速高效
- 强大的分支支持
- 数据完整性

1.2 配置Git

# 设置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

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

# 设置编辑器
git config --global core.editor vim

# 设置别名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit

第二章 基础操作

2.1 创建仓库

# 初始化仓库
git init

# 克隆远程仓库
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git myfolder

2.2 文件状态

工作目录 → 暂存区 → Git仓库
  (modified)  (staged)   (committed)
# 查看状态
git status
git status -s          # 简洁格式

2.3 暂存与提交

# 添加文件
git add file.txt
git add .              # 添加所有
git add *.txt          # 模式匹配
git add -p             # 交互式添加

# 提交
git commit -m "commit message"
git commit -am "message"   # 添加并提交(仅跟踪的文件)

# 查看差异
git diff               # 工作区vs暂存区
git diff --staged      # 暂存区vs仓库
git diff HEAD          # 工作区vs仓库

2.4 查看历史

# 查看提交历史
git log
git log --oneline     # 简洁
git log -n 5          # 最近5条
git log --graph       # 图形化

# 查看具体文件历史
git log file.txt
git log -p file.txt   # 详细
git log --author="name"
git log --since="2024-01-01"

第三章 分支操作

3.1 分支基础

# 查看分支
git branch            # 本地分支
git branch -r         # 远程分支
git branch -a         # 所有分支

# 创建分支
git branch feature    # 创建
git checkout -b feature  # 创建并切换

# 切换分支
git checkout feature
git switch feature    # 新语法

# 删除分支
git branch -d feature     # 删除(已合并)
git branch -D feature     # 强制删除

3.2 合并分支

# 合并分支
git checkout main
git merge feature

# 解决冲突
# 编辑冲突文件
git add resolved_file
git commit

# 使用rebase(变基)
git checkout feature
git rebase main

3.3 标签

# 创建标签
git tag v1.0.0
git tag -a v1.0.0 -m "Release 1.0"

# 查看标签
git tag
git tag -l "v1.*"

# 推送标签
git push origin v1.0.0
git push origin --tags

# 删除标签
git tag -d v1.0.0
git push origin :refs/tags/v1.0.0

第四章 远程仓库

4.1 远程操作

# 查看远程
git remote -v

# 添加远程
git remote add origin https://github.com/user/repo.git

# 推送
git push origin main
git push -u origin main     # 设置上游

# 拉取
git pull origin main
git fetch origin            # 获取不合并

# 克隆
git clone https://github.com/user/repo.git

4.2 远程分支

# 查看远程分支
git branch -r

# 推送分支
git push origin feature

# 删除远程分支
git push origin --delete feature

# 跟踪远程分支
git checkout --track origin/feature
git checkout -b feature origin/feature

第五章 撤销操作

5.1 撤销修改

# 撤销工作区修改
git checkout -- file.txt
git restore file.txt        # 新语法

# 撤销暂存
git reset HEAD file.txt
git restore --staged file.txt

# 撤销提交(未推送)
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1

5.2 恢复删除的提交

# 查看reflog
git reflog

# 恢复
git checkout HEAD@{n}
git branch recovery HEAD@{n}

第六章 储藏(Stash)

# 储藏
git stash
git stash save "work in progress"

# 查看储藏
git stash list

# 恢复储藏
git stash pop          # 恢复并删除
git stash apply        # 恢复保留

# 删除储藏
git stash drop stash@{0}
git stash clear        # 清空所有

第七章 高级技巧

7.1 Cherry-pick

# 挑选单个提交
git cherry-pick abc123

# 挑选多个
git cherry-pick abc123 def456

7.2 Rebase进阶

# 合并提交
git rebase -i HEAD~3

# 变基解决冲突
git rebase --continue
git rebase --abort

7.3 子模块

# 添加子模块
git submodule add https://github.com/user/repo.git path

# 克隆含子模块
git clone --recursive mainrepo

# 初始化子模块
git submodule init
git submodule update

第八章 Git工作流

8.1 Feature Branch

git checkout -b feature/new-feature
git commit -am "feat: add new feature"
git checkout develop
git merge feature/new-feature
git push origin develop

8.2 Git Flow

  • main/master: 正式版本
  • develop: 开发分支
  • feature/*: 功能分支
  • release/*: 发布分支
  • hotfix/*: 紧急修复

8.3 提交规范

类型 说明
feat 新功能
fix 修复bug
docs 文档
style 格式
refactor 重构
test 测试
chore 维护

第九章 常见问题

9.1 解决冲突

# 1. 编辑冲突文件
# 2. 标记已解决
git add .
# 3. 提交
git commit -m "Resolve merge conflict"

9.2 修改commit message

# 最后一次提交
git commit --amend -m "New message"

# 之前的提交
git rebase -i HEAD~n

第十章 常用命令速查

命令 说明
git init 初始化
git clone 克隆
git add 暂存
git commit 提交
git push 推送
git pull 拉取
git status 状态
git log 历史
git branch 分支
git checkout 切换
git merge 合并
git rebase 变基
git stash 储藏
git reset 重置

笔记整理:AI助手
更新时间:2026-03-19


第十章 GitHub Actions

10.1 基础工作流

name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Run tests
      run: |
        pytest tests/

    - name: Upload coverage
      uses: codecov/codecov-action@v3

10.2 Docker工作流

name: Docker Build

on:
  push:
    branches: [main]
    tags: ['v*']

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2

    - name: Login to Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build and push
      uses: docker/build-push-action@v4
      with:
        context: .
        push: ${{ github.event_name != 'pull_request' }}
        tags: user/app:latest

第十一章 企业Git工作流

11.1 Git Flow

main ─●────●────●────●───●───●───
       │    │    │    │   │   │
develop ─●─●─●─●─●─●─●─●─●─●─●─●─●─●─●─
       │    │    │    │
feature ──●─●─●─●─●─
           │    │
release ────●───●───●
               │
hotfix ────────●───●

11.2 GitHub Flow

# 1. 创建分支
git checkout -b feature/my-feature

# 2. 开发并提交
git commit -am "feat: add new feature"

# 3. 开启Pull Request
git push -u origin feature/my-feature

# 4. 代码审查和讨论

# 5. 合并到main

11.3 trunk-based开发

小团队常用模式,主干开发,短期分支。


第十二章 Git钩子

12.1 客户端钩子

# .git/hooks/pre-commit
#!/bin/bash

# 检查是否有调试代码
if git diff --cached --name-only | xargs grep -l 'console.log'; then
    echo "Error: Found console.log in staged files"
    exit 1
fi

# 运行测试
npm test

12.2 服务端钩子

# .git/hooks/pre-receive
#!/bin/bash

while read oldrev newrev refname; do
    branch=$(echo $refname | sed 's|refs/heads/||')
    if [ "$branch" = "main" ]; then
        echo "Direct push to main is not allowed"
        exit 1
    fi
done

第十三章 子模块和子树

13.1 子模块

# 添加子模块
git submodule add https://github.com/user/repo.git path/to/submodule

# 克隆含子模块
git clone --recursive mainrepo

# 更新子模块
git submodule update --remote

# 进入子模块
cd path/to/submodule

13.2 子树

# 添加子树
git subtree add --prefix=libs/repo https://github.com/user/repo.git main --squash

# 更新子树
git subtree pull --prefix=libs/repo https://github.com/user/repo.git main --squash

第十四章 Git最佳实践

14.1 提交信息规范

feat: add user authentication
fix: resolve login issue
docs: update API documentation
style: format code
refactor: simplify algorithm
test: add unit tests
chore: update dependencies

14.2 分支命名

feature/user-authentication
bugfix/login-error
hotfix/security-patch
release/v1.0.0

14.3 常用命令别名

# ~/.gitconfig
[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = log --graph --oneline --all
    amend = commit --amend --no-edit

更新于:2026-03-19