Skip to content

Git 基础与实战

配置

sh
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all --decorate"

查看配置:git config --listgit config user.name

仓库初始化与克隆

sh
# 初始化新仓库
git init

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

# 克隆指定分支
git clone -b develop https://github.com/user/repo.git

日常操作流程

sh
# 查看状态
git status

# 添加文件
git add file.txt          # 添加单个文件
git add .                 # 添加所有变更
git add -p                # 交互式分段添加

# 提交
git commit -m "feat: 添加用户登录功能"

# 推送远程
git push origin main

# 拉取更新
git pull                  # 相当于 git fetch + git merge
git pull --rebase         # 用 rebase 方式拉取
git fetch                 # 仅获取远程变更,不合并

分支管理

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

# 创建分支
git branch feature-login  # 创建
git switch -c feature-login  # 创建并切换(推荐)
git checkout -b feature-login # 旧语法

# 切换分支
git switch main
git checkout main         # 旧语法

# 合并分支
git switch main
git merge feature-login   # 将 feature-login 合并到 main

# 删除分支
git branch -d feature-login     # 本地(已合并)
git branch -D feature-login     # 本地(强制)
git push origin --delete feature-login  # 远程

查看历史

sh
git log                          # 完整日志
git log --oneline                # 一行显示
git log --oneline --graph        # 图形化
git log --oneline --graph --all  # 所有分支
git log --author="name"          # 按作者
git log --since="2025-01-01"     # 按时间
git log -p                       # 显示差异
git log -S "关键词"               # 按关键词搜索

git show commit_hash             # 查看某次提交详情
git diff                         # 工作区 vs 暂存区
git diff --staged                # 暂存区 vs 上次提交
git diff HEAD                    # 工作区 vs 上次提交

撤销变更

sh
# 工作区撤销(未 add)
git restore file.txt
git checkout -- file.txt         # 旧语法

# 暂存区撤销(已 add 但未 commit)
git restore --staged file.txt
git reset HEAD file.txt          # 旧语法

# 撤回上次 commit(保留修改在暂存区)
git reset --soft HEAD~1

# 撤回上次 commit(保留修改在工作区)
git reset --mixed HEAD~1

# 撤回上次 commit(彻底丢弃)
git reset --hard HEAD~1

# 推送到远程后发现错了
git reset --hard HEAD~1
git push --force-with-lease      # 安全覆盖远程

# 如果只是 commit message 写错了
git commit --amend -m "新消息"

Stash(暂存)

sh
git stash                       # 暂存当前工作区
git stash push -m "wip: 登录功能"  # 带说明暂存
git stash list                  # 查看暂存列表
git stash pop                   # 恢复并删除
git stash apply stash@{0}       # 恢复但不删除
git stash drop stash@{0}        # 删除指定暂存
git stash clear                 # 清空所有暂存

Rebase 与 Merge

Merge 常规合并

sh
git switch main
git merge feature-login         # 生成一个合并提交
git merge --no-ff feature-login # 强制生成合并提交(保留分支历史)

Rebase 变基

sh
# 将当前分支的提交挪到 main 的最新提交之后
git switch feature-login
git rebase main

# 如果冲突,解决后:
git add .
git rebase --continue
git rebase --abort              # 放弃 rebase

交互式 Rebase(整理提交历史)

sh
git rebase -i HEAD~3            # 整理最近 3 个提交

交互界面常用操作:

  • pick — 保留该提交
  • reword — 修改提交消息
  • squash — 合并到上一个提交
  • fixup — 合并到上一个提交,丢弃消息
  • drop — 删除该提交
  • edit — 暂停,允许修改该提交内容

Merge vs Rebase 选择

场景推荐方式
合并功能分支到主分支merge --no-ff,保留分支结构
拉取远程最新代码pull --rebase,避免多余合并提交
整理本地未推送的提交交互式 rebase -i
多人协作的公共分支不要 rebase,用 merge

.gitignore

常见模板:

text
node_modules/
dist/
.env
*.log
.DS_Store
.vscode/
.idea/
*.local

创建后生效需先清理缓存:

sh
git rm -r --cached node_modules/

Tag 管理

sh
# 创建标签
git tag v1.0.0
git tag -a v1.0.0 -m "正式版 1.0.0"   # 带注释

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

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

# 删除标签
git tag -d v1.0.0                    # 本地
git push origin :refs/tags/v1.0.0    # 远程
git push origin --delete v1.0.0      # 远程(同上)

远程仓库

sh
# 查看远程
git remote -v

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

# 修改远程地址
git remote set-url origin git@github.com:user/repo.git

# 删除远程
git remote remove origin

子模块

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

# 克隆带子模块的项目
git clone --recursive https://github.com/user/project.git

# 更新子模块
git submodule update --init --recursive

常用工作流:Feature Branch

main ──┬─── merge ────────────── 稳定分支
       │         ↖
dev ───┬── merge ──┬── merge ─── 开发分支
      │         ↖        ↖
     feat/A    feat/B        开发分支
sh
# 从 main 创建功能分支
git switch main
git pull
git switch -c feat/user-login

# 开发并提交
git add .
git commit -m "feat: 登录功能"

# 拉取最新 main 并 rebase
git fetch origin
git rebase origin/main

# 推送到远程并创建 PR
git push -u origin feat/user-login

常见问题

问题解决
提交后想改 messagegit commit --amend
漏了文件git add 漏的文件 && git commit --amend --no-edit
提交到了错误分支git reset HEAD~1 && git switch 正确分支 && git add . && git commit -c ORIG_HEAD
误删了文件git checkout HEAD -- file.txtgit restore file.txt
想拆分一个提交git reset HEAD~1,再分次 add -p + 多次 commit
合并冲突手动解决,git add .git commit(merge)或 git rebase --continue(rebase)
误操作 hard resetgit reflog 找到 reset 前的 commit,git reset --hard 那个commit

Reflog(后悔药)

sh
git reflog               # 查看所有 HEAD 移动记录
git reset --hard HEAD@{2}  # 恢复到任意历史位置
git checkout HEAD@{1}    # 查看之前的状态

reflog 是 Git 的救命稻草,哪怕是 --hard 操作也能恢复。

蜀ICP备2025150039号