Git 参考文档

本文档整理了常用的 Git 指令,按功能分类,方便快速查阅和使用。

1. 基础配置

全局配置

  • git config --global user.name "Your Name"

    • 设置全局用户名
    • 示例:git config --global user.name "张三"
  • git config --global user.email "your_email@example.com"

    • 设置全局邮箱
    • 示例:git config --global user.email "zhangsan@example.com"
  • git config --global core.editor "editor_name"

    • 设置默认文本编辑器
    • 示例:git config --global core.editor "vim"
  • git config --global alias.alias_name "command"

    • 设置命令别名
    • 示例:git config --global alias.st "status"
  • git config --list

    • 查看当前所有配置

局部配置

  • git config user.name "Your Name"

    • 设置当前仓库用户名
  • git config user.email "your_email@example.com"

    • 设置当前仓库邮箱

2. 仓库操作

创建仓库

  • git init

    • 初始化一个新的 Git 仓库
  • git clone <repository_url>

    • 克隆远程仓库到本地
    • 示例:git clone https://github.com/user/repo.git

查看状态

  • git status

    • 查看当前仓库状态
  • git log

    • 查看提交历史
    • 常用选项:
      • -p:显示每次提交的内容差异
      • --oneline:简洁显示每条提交
      • --graph:以图形化方式显示提交历史
  • git show <commit_hash>

    • 查看指定提交的详细信息

3. 分支管理

分支操作

  • git branch

    • 查看本地分支
    • 选项:
      • -a:查看所有分支(包括远程)
      • -r:查看远程分支
  • git branch <branch_name>

    • 创建新分支
    • 示例:git branch feature/new-feature
  • git checkout <branch_name>

    • 切换到指定分支
    • 示例:git checkout feature/new-feature
  • git checkout -b <branch_name>

    • 创建并切换到新分支
  • git merge <branch_name>

    • 合并指定分支到当前分支
  • git branch -d <branch_name>

    • 删除本地分支
    • 选项:
      • -D:强制删除未合并的分支
  • git push origin --delete <branch_name>

    • 删除远程分支

4. 提交管理

工作区与暂存区

  • git add <file>

    • 将文件添加到暂存区
    • 示例:git add index.html
    • 选项:
      • .:添加所有修改的文件
  • git add -p <file>

    • 交互式添加文件的部分修改
  • git reset <file>

    • 将文件从暂存区移除
  • git diff

    • 查看工作区与暂存区的差异
  • git diff --staged

    • 查看暂存区与上一次提交的差异

提交

  • git commit -m "commit_message"

    • 提交暂存区的文件
    • 示例:git commit -m "Add new feature"
  • git commit -a -m "commit_message"

    • 跳过暂存区,直接提交所有修改的文件
  • git commit --amend

    • 修改最近一次提交

5. 远程仓库

远程仓库管理

  • git remote -v

    • 查看远程仓库信息
  • git remote add <remote_name> <repository_url>

    • 添加远程仓库
    • 示例:git remote add origin https://github.com/user/repo.git
  • git remote rename <old_name> <new_name>

    • 重命名远程仓库
  • git remote remove <remote_name>

    • 删除远程仓库

推送与拉取

  • git push <remote_name> <branch_name>

    • 将本地分支推送到远程仓库
    • 示例:git push origin main
    • 选项:
      • -u:设置上游分支,后续可直接使用 git push
  • git pull <remote_name> <branch_name>

    • 从远程仓库拉取并合并到本地分支
  • git fetch <remote_name>

    • 从远程仓库拉取最新代码,但不合并

6. 撤销与回滚

撤销修改

  • git checkout -- <file>

    • 撤销工作区的修改
    • 示例:git checkout -- index.html
  • git reset HEAD <file>

    • 将文件从暂存区撤销到工作区

回滚提交

  • git revert <commit_hash>

    • 创建一个新提交,撤销指定提交的更改
  • git reset --hard <commit_hash>

    • 重置当前分支到指定提交,丢弃所有更改
  • git reset --soft <commit_hash>

    • 重置当前分支到指定提交,保留更改在暂存区
  • git reset --mixed <commit_hash>

    • 重置当前分支到指定提交,保留更改在工作区

7. 标签管理

标签操作

  • git tag

    • 查看所有标签
  • git tag <tag_name>

    • 创建轻量级标签
    • 示例:git tag v1.0.0
  • git tag -a <tag_name> -m "tag_message"

    • 创建带注释的标签
    • 示例:git tag -a v1.0.0 -m "Version 1.0.0 release"
  • git push <remote_name> <tag_name>

    • 推送指定标签到远程仓库
  • git push <remote_name> --tags

    • 推送所有标签到远程仓库
  • git tag -d <tag_name>

    • 删除本地标签
  • git push <remote_name> --delete <tag_name>

    • 删除远程标签

8. 高级操作

变基

  • git rebase <branch_name>

    • 将当前分支的提交变基到指定分支
  • git rebase -i <commit_hash>

    • 交互式变基,可修改提交历史

暂存

  • git stash

    • 暂存当前工作区的修改
  • git stash list

    • 查看所有暂存的修改
  • git stash apply

    • 应用最近的暂存修改
  • git stash apply stash@{n}

    • 应用指定的暂存修改
  • git stash pop

    • 应用最近的暂存修改并删除暂存
  • git stash drop stash@{n}

    • 删除指定的暂存修改

子模块

  • git submodule add <repository_url> <path>

    • 添加子模块
  • git submodule init

    • 初始化子模块
  • git submodule update

    • 更新子模块

9. 实用技巧

  • git log --oneline --graph --all

    • 以图形化方式查看所有分支的提交历史
  • git blame <file>

    • 查看文件每行的修改历史
  • git cherry-pick <commit_hash>

    • 将指定提交应用到当前分支
  • git bisect start

    • 开始二分查找定位引入问题的提交
  • git bisect good <commit_hash>

    • 标记当前提交为正常
  • git bisect bad <commit_hash>

    • 标记当前提交为有问题
  • git bisect reset

    • 结束二分查找

总结

本文档涵盖了 Git 的常用指令,包括基础配置、仓库操作、分支管理、提交管理等多个方面。通过这些指令,您可以有效地使用 Git 进行版本控制。

如果需要更详细的信息,可以使用 git help <command> 查看特定指令的完整文档,例如:git help commit

最后更新:2026-02-05