git-note

GIT NOTE

参考资料:

  • 官方文档
  • 教学笔记
  • 教学网站

命令速查表

命令 功能 命令格式 实例
init 初始化仓库 git init git init
clone 克隆远程库到本地 git clone <url> git clone git@github.com:iLogIng/Repo-name.git
add 将文件加入暂存区 git add <file> git add README.md
add 将全部修改加入暂存区 git add . git add .
commit 提交暂存区内容 git commit -m "<描述>" git commit -m "Add the README.md file into git Repo"
status 查看仓库当前状态 git status git status
diff 查看未提交的修改 git diff git diff
diff 对比工作区与版本库 git diff HEAD -- <file> git diff HEAD -- README.md
log 查看提交日志 git log git log
log 图形化查看所有分支的提交历史 git log --oneline --graph --all git log --oneline --graph --all
reset 回退到指定版本的已提交状态 git reset --hard <commit_id> git reset --hard HEAD^
reset 撤销暂存区的修改 git reset HEAD <file> git reset HEAD README.md
reflog 查看所有提交日志,找回历史 git reflog git reflog
checkout 丢弃工作区的修改 git checkout -- <file> git checkout -- README.md
branch 查看本地分支 git branch git branch
branch 查看本地与远程的全部分支 git branch -a git branch -a
branch 创建分支 git branch <name> git branch feature/login
switch 切换分支 git switch <name> git switch main
switch 创建并切换分支 git switch -c <name> git switch -c feature/add-header
checkout 创建并切换分支(传统写法) git checkout -b <name> git checkout -b feature/add-header
merge 将分支合并到当前分支 git merge <name> git merge feature/login
branch 删除已合并的本地分支 git branch -d <name> git branch -d feature/login
push 删除远程分支 git push origin --delete <name> git push origin --delete feature/login
stash 暂存工作区未提交的修改 git stash git stash
stash 查看暂存列表 git stash list git stash list
stash 恢复最近一次暂存 git stash pop git stash pop
stash 丢弃某次暂存 git stash drop git stash drop
fetch 拉取远程更新,不自动合并 git fetch <remote> git fetch origin
pull 拉取并合并远程更新 git pull <remote> <branch> git pull origin main
push 推送本地提交到远程 git push <remote> <branch> git push origin main
remote 查看远程仓库信息 git remote -v git remote -v
remote 添加远程库 git remote add origin <url> git remote add origin git@github.com:iLogIng/RepoName.git
remote 删除远程库绑定 git remote rm <name> git remote rm origin
tag 打轻量标签 git tag <tag> git tag v1.0.0
tag 打附注标签 git tag -a <tag> -m "<描述>" git tag -a v1.0.0 -m "release v1.0.0"
push 推送标签到远程 git push origin <tag> git push origin v1.0.0
config 设置提交者姓名 git config --global user.name <name> git config --global user.name "iLogIng"
config 设置提交者邮箱 git config --global user.email <email> git config --global user.email "email@example.com"

基础版本控制

以下操作均在测试仓库 test-dir/ 中进行:

1
mkdir test-dir

1. 添加仓库

使用 git init 初始化仓库,此时文件夹 test-dir/ 已绑定到一个 Git Repo:

1
./test-dir $: git init

2. 将文件添加到版本库

  1. 使用 git add [filename] 将文件加入版本库
  2. 使用 git commit -m “describe” 将已添加的文件提交到仓库,提交描述为 “describe”
1
2
./test-dir $: git add README.md
./test-dir $: git commit -m "Add the README.md file into git Repo"
  • commit 相当于为仓库添加一个版本快照,并对快照进行管理

3. 查看仓库状态

使用 git status 查看仓库当前状态:

1
./test-dir $: git status

使用 git diff 查看已修改但未提交的内容:

1
./test-dir $: git diff

4. 切换快照版本

使用 git log 查看从最近到最远的提交日志,使用 git reset --hard commit_id 回到指定版本的已提交状态:

1
2
./test-dir $: git log
./test-dir $: git reset --hard commit_id
  • –soft:回到上个版本的未提交状态
  • –mixed:回退到上个版本的已添加未提交状态
  • commit_id 可以是快照 id,也可以是 HEAD^(上个版本)、HEAD^^(向上第 2 个版本)、HEAD~N(向上第 N 个版本);也支持填写部分 commit_id,由系统自动匹配
  • 使用 git reflog 查看从近到远的所有提交日志,可以找回过去的提交内容

5. 管理项目分支

项目管理与协作中,分支是最核心的工具,配合远程同步、暂存、标签等命令完成协作流程。

查看分支

1
2
git branch          # 查看本地分支,当前分支前带 *
git branch -a # 查看本地与远程的全部分支

创建与切换分支

1
2
3
4
git branch feature/login           # 创建分支
git switch feature/login # 切换分支(推荐)
git switch -c feature/add-header # 创建并切换分支(推荐)
git checkout -b feature/add-header # 创建并切换分支(传统写法)

合并分支

1
2
3
4
5
6
# 先切换到目标分支,再合并
git switch main
git merge feature/login

git branch -d feature/login # 删除已合并的本地分支
git push origin --delete feature/login # 删除远程分支

暂存工作区修改(stash)

需要切换分支前,可先将未提交的修改暂存起来:

1
2
3
4
git stash          # 暂存工作区未提交的修改
git stash list # 查看暂存列表
git stash pop # 恢复最近一次暂存
git stash drop # 丢弃某次暂存

与远程仓库同步

1
2
3
4
git fetch origin      # 拉取远程更新,不自动合并
git pull origin main # 拉取并合并(等价于 fetch + merge)
git push origin main # 推送本地提交到远程
git remote -v # 查看远程仓库信息

标签(tag)

1
2
3
git tag v1.0.0                 # 打轻量标签
git tag -a v1.0.0 -m "版本描述" # 打附注标签
git push origin v1.0.0 # 推送标签到远程

查看提交历史

1
git log --oneline --graph --all   # 图形化查看所有分支的提交历史

工作区和暂存区

工作区

工作区就是工作文件夹。

暂存区

工作区中名为 .git 的隐藏目录不是工作区,而是 Git 的版本库。

  • git add filenames 实际上是将 filenames 加入暂存区
  • git commit -m “describe” 实际上是将暂存区的内容提交到当前分支

管理修改

Git 管理的是修改而非文件。可以使用 git diff HEAD -- filename 对比当前工作区与版本库中的内容:

1
git diff HEAD -- filename

撤销修改

丢弃工作区的更改,恢复到最近一次 commit 或 add 时的状态:

1
git checkout -- file

撤销暂存区的修改:

1
git reset HEAD filename

远程仓库

Git 仓库与 GitHub 仓库之间的传输通过 SSH 加密,需要先进行设置。

1. 创建 SSH Key

在用户主目录下查看是否存在 .ssh 目录,若存在且目录下有 id_rsa 与 id_rsa.pub 文件,可直接从第 2 步开始;否则创建 SSH Key:

1
ssh-keygen -t rsa -C "email@example.com"

将 email@example.com 换为自己的邮箱地址。

  • id_rsa 是私钥
  • id_rsa.pub 是公钥

2. 在 GitHub 中添加 SSH 密钥

在 Account / SSH and GPG keys 中,点击 New SSH key,填入 id_rsa.pub 文件中的公钥内容。

添加远程库

若本地已创建 Git 仓库,又想在 GitHub 上创建仓库并保持远程同步,可以添加远程库:

  1. 先在 GitHub 上通过 Create a new repo 创建一个仓库
  2. 再在本地仓库中运行:
1
git remote add origin git@github.com:iLogIng/RepoName.git
  1. 添加后远程库的名字为 origin,可以进行更名
  2. 将本地库的所有内容推送到远程库:
1
git push -u origin master

第一次推送时远程库为空,需要加上 -u 选项。之后只要本地做了提交,就可以直接推送最新修改:

1
git push origin master

删除远程库:若添加时地址写错或想删除远程库,先使用 git remote -v 查看远程库信息,再使用 git remote rm <name> 删除,例如:

1
2
git remote -v
git remote rm origin

此处的删除只是解除了本地与远程的绑定关系,并不会物理删除远程库;真正删除远程库需要在 GitHub 网页上操作。

从远程库克隆

使用以下命令在本地克隆一个远程库:

1
git clone git@github.com:iLogIng/Repo-name.git

提交信息规范

在团队协作或开源项目中,规范的 Git 提交信息能极大提升代码库的可读性与可追溯性,便于自动生成 CHANGELOG 和版本管理。目前最主流的规范是 Conventional Commits(基于 Angular 规范),格式如下:

1
2
3
4
5
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

1. 常用类型(type)

类型 说明
feat 新功能(feature)
fix 修复 bug
docs 文档变更
style 代码格式(不影响代码运行的变动,如空格、缩进等)
refactor 重构(既不是新功能也不是修复 bug)
perf 性能优化
test 增加或修改测试
chore 构建过程、辅助工具或依赖库的变动(如修改构建脚本、升级依赖)
revert 回滚某个提交

2. 作用域(scope)

  • 可选,表示本次修改影响的范围(如模块、文件名、组件名)
  • 例如:feat(allocator): add boundary tag coalesce
  • 若修改涉及多个模块,可以省略 (scope)

3. 主题(subject)

  • 必须使用祈使句(动词开头,如 add、fix、update),不使用过去式
  • 首字母小写,结尾不加句号
  • 长度尽量控制在 50 字符以内,简洁概括改动

4. 正文(body)

  • 可选,用于详细描述修改的原因、如何修改、影响范围等
  • 每行不超过 72 字符
  • 与主题之间空一行

5. 脚注(footer)

  • 可选,通常用于说明 Breaking Changes 或关联 Issue / PR
  • 格式:
    BREAKING CHANGE: <description>
    Closes #123, #456

基本工作流参考

确认分支

1
2
git branch
git status

拉取最新代码

1
git pull

创建 / 切换分支

1
2
3
git branch feature/login         # 创建分支
git switch feature/login # 切换分支
git switch -c feature/add-header # 创建并切换分支

个人提交

1
2
git add .
git commit -m "feat: 添加页面头部组件"

修复 Bug

1
2
3
4
5
6
7
8
9
# 若某个分支存在 Bug,先切换分支并拉取分支最新代码
git switch main
git pull # 确保最新

git switch -c hotfix/fix-typo # 创建修复分支

# 修复 Bug 并提交
git add .
git commit -m "fix: 修正首页文字错别字"

分支合并

1
2
3
4
5
# 切回主分支 main 合并修复
git switch main
git merge hotfix/fix-typo
git push origin main
git branch -d hotfix/fix-typo # 删除已合并的热修复分支

切回开发分支并最终合并

1
2
3
4
5
6
7
8
9
10
11
# 继续开发之前的头部功能(切回 feature 分支)
git switch feature/add-header

# 开发完毕,最终合并到主分支 main
git switch main
git pull # 再次确保最新(避免别人先合了代码)
git merge feature/add-header

# 若没有冲突,推送到远程并删除本地功能分支
git push origin main
git branch -d feature/add-header

Adds

git submodule 可以将其他仓库作为子模块添加到该仓库

如,添加 Dear ImGui 作为自己项目的子模块

1
git submodule add -b docking https://github.com/ocornut/imgui.git include/imgui

这样,在克隆仓库的同时,会生成一个 .gitmodules 的配置文件在仓库根目录下,记录了项目的子模块依赖信息


END