Skip to content

Git 速查手册

Git 是一个开源的分布式版本控制系统,由 Linus Torvalds 于 2005 年创建,最初用于 Linux 内核开发。它高效、灵活,能够处理从小型到超大型的项目版本管理。

1. 核心优势

  • 分布式开发:每个开发者本地都保留完整仓库,强调个体工作流。
  • 速度快、灵活:本地操作几乎瞬时完成,分支切换成本极低。
  • 冲突处理方便:任意两个开发者之间可以较容易地解决合并冲突。
  • 减轻服务器压力:大多数操作在本地完成,公共服务器负载与数据量更小。

2. 常用命令

2.1 配置(config)

查看全部配置:

shell
git config --list

配置全局用户名与邮箱:

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

查看当前用户名与邮箱:

shell
git config user.name
git config user.email

设置别名以简化常用命令:

shell
git config --global alias.a   add
git config --global alias.co  checkout
git config --global alias.ci  commit
git config --global alias.br  branch
git config --global alias.st  status

# 使用示例
git co main

2.2 新建仓库

从远程克隆:

shell
git clone git@github.com:username/repository.git
cd repository
touch README.md
git add README.md
git commit -m "add README"
git push -u origin main

本地初始化:

shell
echo "# repository" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:username/repository.git
git push -u origin main

推送已有仓库:

shell
git remote add origin git@github.com:username/repository.git
git push -u origin main

2.3 拉取与推送代码

克隆仓库:

shell
git clone https://github.com/user/test.git

推送代码:

shell
# 提交到默认远程分支
git push origin main

# 强制推送(会覆盖远程历史,慎用)
git push origin main -f

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

远程仓库管理:

shell
# 查看已关联的远程仓库名称
git remote

# 查看远程仓库详情
git remote -v

# 删除远程仓库关联
git remote remove <name>

# 修改远程仓库地址
git remote set-url origin <new-url>

# 刷新远程分支列表
git remote update origin --prune

# 添加新的远程仓库
git remote add upstream https://github.com/user/repo.git

拉取更新:

shell
# 拉取并合并当前分支对应的远程分支
git pull origin main

2.4 基本信息操作

shell
# 将当前路径下所有修改加入暂存区
git add .

# 查看工作区与暂存区状态
git status

# 提交变更
git commit -m "描述信息"

# 比较工作区与暂存区差异
git diff <file>

# 比较工作区与最新提交差异
git diff HEAD -- <file>

# 撤销最近一次提交(保留修改)
git reset --soft HEAD~1

# 回退到指定版本(丢弃工作区修改,慎用)
git reset --hard <commit-hash>

注意--hard 会丢弃工作区与暂存区的修改;--soft 仅回退提交历史,并保留修改到暂存区。

2.5 分支操作

shell
# 查看本地分支
git branch

# 查看本地与远程分支
git branch -a

# 查看远程分支
git branch -r

# 创建 dev 分支
git branch dev

# 创建并切换到新分支
git checkout -b <branch>

# 删除已合并的本地分支
git branch -d dev

# 强制删除未合并的本地分支
git branch -D test

# 重命名分支
git branch -m old-name new-name

# 创建指向指定提交的分支
git branch <branch> <commit-hash>

# 与指定远程分支建立追踪关系
git branch --set-upstream-to <remote>/<branch>

# 查看已合并到当前分支的分支
git branch --merged

# 查看未合并到当前分支的分支
git branch --no-merged

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

2.6 切换分支与恢复文件

shell
# 恢复暂存区文件到工作区
git checkout -- <file>

# 切换到 dev 分支
git checkout dev

# 创建并切换到 dev 分支
git checkout -b dev

# 变基到 master
git rebase master

# 合并指定分支到当前分支
git merge test

# 将指定分支的某个提交应用到当前分支
git cherry-pick <commit-hash>

# 撤销当前 merge
git revert -m 1 <merge-commit-hash>

# 撤销指定提交
git revert <commit-hash>

2.7 子模块(submodule)

shell
# 添加子模块
git submodule add <url> <name>
git add <name>
git commit -m "add submodule"
git push

# 拉取包含子模块的仓库
git pull --recurse-submodules

2.8 SSH 密钥

生成 SSH 密钥:

shell
ssh-keygen -t rsa -C "your_email@example.com"

默认会在 ~/.ssh 目录下生成 id_rsaid_rsa.pub 两个文件。将 id_rsa.pub 的内容复制到 GitHub 的 Settings → SSH and GPG keys → New SSH key 中,然后在终端验证:

shell
ssh -T git@github.com

2.8.1 SSH 端口配置

若 22 端口被屏蔽或占用,GitHub 提供 443 端口备用:

shell
ssh -T -p 443 git@ssh.github.com

持久化配置可写入 ~/.ssh/config

shell
Host github.com
  Hostname ssh.github.com
  Port 443

2.9 查看日志

shell
# 一行显示提交历史
git log --oneline

# 查看某文件每次提交的详细 diff
git log -p <file>

# 查看最近两次提交的 diff
git log -p -2

# 查看变化的文件列表
git log --name-only

# 查看文件变化状态
git log --name-status

# 显示每次提交的统计信息
git log --stat

# 显示某次提交详情
git show <commit-hash>

# 查看文件每行最后由谁修改
git blame <file>

# 修改最近一次提交信息
git commit --amend

# 提交时显示完整 diff
git commit -v

2.10 回退与撤销

shell
# 撤销工作区修改
git checkout -- <file>

# 将暂存区文件移回工作区
git restore --staged <file>

# 用最新提交覆盖工作区与暂存区
git checkout HEAD -- <file>

# 撤销最近一次提交,保留修改到暂存区
git reset --soft HEAD~1

# 撤销最近一次提交,保留修改到工作区
git reset HEAD~1

# 丢弃所有未提交的本地修改(慎用)
git reset --hard

# 丢弃所有未推送的本地提交(慎用)
git reset --hard @{u}

2.11 标签(tag)

shell
# 为当前 HEAD 创建标签
git tag v1.0

# 为指定提交创建标签
git tag v2.0 <commit-hash>

# 创建带说明的附注标签
git tag -a v3.0 -m "version 3.0 released"

# 查看标签详情
git show v0.2

# 列出所有标签
git tag

# 删除本地标签
git tag -d v1.0

# 推送指定标签到远程
git push origin v0.9

# 推送所有本地标签到远程
git push origin --tags

# 删除远程标签(先删本地,再删远程)
git tag -d v0.9
git push origin :refs/tags/v0.9

2.12 暂存区(stash)

shell
# 将当前修改放入 stash
git stash

# 查看 stash 列表
git stash list

# 恢复最近一次 stash,不删除
git stash apply

# 恢复指定 stash
git stash apply stash@{0}

# 恢复最近一次 stash 并删除
git stash pop

# 删除指定 stash
git stash drop stash@{0}

3. 同步 Fork 仓库

当 Fork 的原始仓库有了新更新,而你的 Fork 尚未同步时,可以通过以下步骤保持同步。

3.1 步骤 1:克隆 Fork 仓库

shell
git clone https://github.com/your-username/mmdetection.git
cd mmdetection
git remote -v

3.2 步骤 2:添加上游仓库

shell
git remote add upstream https://github.com/open-mmlab/mmdetection.git
git remote -v

3.3 步骤 3:获取上游更新

shell
git fetch upstream

3.4 步骤 4:合并到本地分支

shell
git checkout main
git merge upstream/main

若本地分支没有独有提交,Git 会执行“快进合并”(Fast-forward)。

4. Git 钩子(Hooks)

Git 能在特定动作发生时触发自定义脚本,分为客户端钩子与服务器端钩子。将正确命名且可执行的文件放入 .git/hooks 目录即可激活。

4.1 常用客户端钩子

shell
pre-commit          # 提交前运行
prepare-commit-msg  # 启动提交信息编辑器后、默认信息生成后运行
commit-msg          # 校验提交信息
post-commit         # 提交完成后运行
post-applypatch     # git am 产生提交后运行
pre-rebase          # 变基前运行
post-rewrite        # git commit --amend / git rebase 后运行
pre-push            # push 时、更新远程引用后运行

4.2 常用服务器端钩子

shell
pre-receive  # 接收推送时最先调用
update       # 为每个准备更新的分支各运行一次
post-receive # 推送过程完结后运行

5. .gitignore 配置

.gitignore 对其所在目录及所有子目录生效。通过将它提交到仓库,可以共享统一的忽略规则。

5.1 配置语法

  • # 开头表示注释;
  • / 开头表示目录;
  • * 通配多个字符;
  • ? 通配单个字符;
  • [] 包含单个字符匹配列表;
  • ! 表示不忽略(跟踪)匹配到的文件或目录。

示例:

text
# 忽略所有 .log 文件
*.log

# 但保留 important.log
!important.log

# 忽略 build 目录
/build/

# 忽略所有 .o 与 .a 文件
*.[oa]

6. Git 插件

6.1 pre-commit

pre-commit 是一个 Git 钩子工具,可以在提交前运行指定的检查脚本,防止不规范代码进入仓库。与 husky 相比功能较聚焦,但可以通过安装 pre-push 等插件扩展对应的 Git 操作检查。

使用 npm 包 pre-commit 的示例:

shell
npm install pre-commit --save-dev

package.json 中配置:

json
{
  "scripts": {
    "test": "jest --coverage --coverageDirectory=testreport"
  },
  "pre-commit": {
    "run": "test"
  }
}

7. 私有 Git 仓库搭建方案

Maintained by Robin