1. 引言
Git 是现代软件开发中事实上的版本控制系统。无论你是独立开发者,还是大型开源社区的一员,精通 Git 都能显著改善你的工作流、协作效率与代码质量。下面按步骤走一遍核心要点——克隆、分支、合并、变基,以及一些专业级技巧——示例都保持简短实用。
2. 起步:第一次提交
# Initialize a new repository
mkdir my‑project && cd my‑project
git init
# Create a README and commit it
printf "# My Project\n\nA brief description." > README.md
git add README.md
git commit -m "Initial commit"
为什么需要 git add?
- 把文件暂存到下一次提交。
- 允许你只提交一部分改动,方便处理只做了一半的工作。
3. 克隆与远程工作流
# Clone an existing repo
git clone https://github.com/your‑org/awesome‑app.git
cd awesome‑app
# List configured remotes
git remote -v
常见的远程名称:
origin——clone创建的默认远程。upstream—— 指向原始上游仓库的第二个远程,fork 场景里很常见。
添加一个新的远程
git remote add upstream https://github.com/original/awesome‑app.git
这样就能拉取上游的改动,而不影响自己的 origin 分支。
4. 分支:并行开发的核心
# Create and switch to a new feature branch
git checkout -b feature/login
# Do some work, then commit
git add src/login.js
git commit -m "Add login component"
分支命名约定
feature/*—— 新功能。bugfix/*—— 缺陷修复。hotfix/*—— 紧急的生产环境补丁。release/*—— 发布前的准备。
让分支保持短生命周期,能减少合并冲突,也让历史更清爽。
5. 合并与变基
合并(保留历史)
git checkout main
git merge feature/login # Creates a merge commit
变基(线性历史)
git checkout feature/login
git rebase main # Replay commits on top of main
何时该变基?
- 在发起 Pull Request(PR)之前,让 PR 保持整洁。
- 绝不要对同事已经拉取过的共享分支做变基。
何时该合并?
- 用于长期存在的分支,并且你希望保留原始上下文时。
6. 交互式变基:整理提交
假设你有三个凌乱的提交,想把它们压缩成一个连贯的提交。
git log --oneline # Identify the commit hash before the three
# Assume the hash is abc123
git rebase -i abc123
编辑器会打开:
pick e1a2b3 Add login UI
pick f4c5d6 Fix login typo
pick a7b8c9 Add unit tests
把第二行和第三行的 pick 改成 squash(或 s),保存,再编辑合并后的提交信息。
7. 暂存:保存进行中的工作
需要切换分支但还不想提交时:
git stash push -m "WIP: login UI"
# Switch branches, work, then restore
git stash list
git stash apply stash@{0}
也可以用 git stash pop,一步完成「应用并删除」该暂存。
8. 解决冲突
Git 无法自动合并改动时就会产生冲突。在一次撞上冲突的 merge 或 rebase 之后:
# Open the conflicted file; you'll see markers like:
<<<<<<< HEAD
// code from current branch
=======
// code from incoming branch
>>>>>>> feature/login
# Edit to keep the correct version, then:
git add conflicted-file.js
# Continue the operation
git merge --continue # or git rebase --continue
提示:用 git diff --conflict 看并排对比视图。
9. 为发布打标签
# Annotated tag (recommended for releases)
git tag -a v1.0.0 -m "First stable release"
# Push tags to remote
git push origin v1.0.0
附注标签会保存打标签者的名字、邮箱、日期和一条消息,很适合拿来写发布说明。
10. 专业技巧与鲜为人知的命令
git log --graph --oneline --decorate:在终端里可视化分支结构。git reflog:通过查看引用日志找回丢失的提交。git cherry-pick <hash>:把另一个分支的单个提交应用过来。git bisect start…git bisect bad/good:用二分查找定位引入缺陷的那次提交。git filter-branch(或git filter-repo):重写历史,例如删除大文件。
示例:用 git bisect 定位缺陷
# Start bisect on the range where bug appeared
git bisect start
git bisect bad # Current HEAD is bad
git bisect good v1.2.3 # Tag known good version
# Git checks out a mid‑point; test, then tell Git:
# If bad:
git bisect bad
# If good:
git bisect good
# Repeat until Git pinpoints the offending commit
11. 用钩子实现自动化
Git 钩子让你在关键节点跑脚本。例如强制约束提交信息的格式:
# .git/hooks/commit-msg (make it executable)
#!/usr/bin/env bash
msg=$(cat "$1")
if ! grep -qE "^JIRA-[0-9]+:" <<< "$msg"; then
echo "Commit message must start with a JIRA ticket (e.g., JIRA-123: ...)" >&2
exit 1
fi
现在任何不以工单号开头的 git commit 都会被中止。
12. 结语
Git 是一个强大的工具箱,而不只是一条命令。把基础打牢——克隆、分支、合并/变基——再点缀一些进阶技巧,比如交互式变基、bisect 和钩子,仓库会保持干净、历史会保持可读、团队也会更高效。
从今天开始套用这些模式,开发工作流会更快、更安全,也远比现在省心。
觉得有用就点赞、点个「在看」,顺手关注一下,后面继续聊 Git 与运维的实战笔记。