Git 是一个分布式版本控制系统(DVCS),由 Linus Torvalds 于 2005 年创建,最初用于 Linux 内核的协同开发。Git 的设计目标是 快速、高效、分布式,它允许每个开发者拥有完整的代码库副本,无需中心服务器即可工作。
Git 的核心定位是 分布式版本控制和代码协作。它提供了:
2005 年,Linus Torvalds 与 BitKeeper(Linux 内核之前的版本控制工具)的授权关系破裂。Linus 决定开发一个更好的版本控制工具,Git 在两周内便初具雏形。Git 的设计目标是:速度快、分布式、数据完整、支持非线性开发。
# 初始化新仓库
git init
# 克隆远程仓库
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git # SSH
# 克隆指定分支
git clone -b feature-branch https://github.com/user/repo.git
# 查看状态
git status
# 查看文件变更
git diff
git diff --cached # 查看已暂存的变更
# 添加文件到暂存区
git add filename
git add . # 添加所有文件
git add -p # 交互式添加
# 提交
git commit -m "Commit message"
git commit -am "Commit all and message" # 跳过 add
# 查看提交历史
git log
git log --oneline
git log --graph --pretty=oneline
# 创建分支
git branch feature-branch
# 切换分支
git checkout feature-branch
git switch feature-branch # 新语法
# 创建并切换
git checkout -b feature-branch
git switch -c feature-branch # 新语法
# 查看所有分支
git branch
git branch -a # 包含远程分支
# 删除分支
git branch -d feature-branch
# 合并分支
git checkout main
git merge feature-branch
# 变基(Rebase)
git checkout feature-branch
git rebase main
git rebase -i HEAD~3 # 交互式变基
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 推送代码
git push origin main
git push -u origin main # 设置默认上游
# 拉取代码(等价于 fetch + merge)
git pull origin main
# 获取远程更新(不合并)
git fetch origin
# 删除远程分支
git push origin --delete feature-branch
# 撤销工作区修改(恢复到上次提交)
git restore filename
git checkout -- filename # 旧语法
# 撤销暂存区(unstage)
git restore --staged filename
git reset HEAD filename # 旧语法
# 撤销最后一次提交(保留修改)
git reset --soft HEAD~1
# 撤销最后一次提交(丢弃修改)
git reset --hard HEAD~1
# 撤销到指定提交
git reset --hard commit_hash
# 用 revert 创建反向提交
git revert commit_hash
# 修改最后一次提交信息
git commit --amend -m "New message"
# 挑选提交(Cherry-pick)
git cherry-pick commit_hash
# 暂存修改(Stash)
git stash
git stash save "WIP" # 带消息
git stash list
git stash apply
git stash pop
git stash drop
# 标签(Tag)
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
# 二分查找(Bisect)
git bisect start
git bisect bad HEAD
git bisect good commit_hash
# 标记当前版本好坏,直到找到问题提交
# 清理未跟踪文件
git clean -n # 预览
git clean -f # 强制执行
# 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 配置编辑器
git config --global core.editor "vim"
# 设置别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)%an%Creset'"
# 查看所有配置
git config --list
git config --global --list
| 工作流 | 复杂度 | 适用场景 |
|---|---|---|
| Git Flow | 高 | 大型项目、有版本发布周期 |
| GitHub Flow | 低 | 持续部署、SaaS 项目 |
| GitLab Flow | 中 | 多环境部署、企业项目 |
Git 安装、基本概念(仓库、提交、分支)、init/clone/add/commit/push/pull
分支管理、合并策略、冲突解决、rebase、stash
cherry-pick、bisect、reflog、hook、submodule
Git Flow、GitHub Flow、GitLab Flow、Pull Request 流程
Git 是软件开发者的必备技能。
无论你使用哪种编程语言、在哪个平台开发,Git 都是协作开发的基石。它用分布式架构、轻量级分支、强大的合并能力,彻底改变了团队协作的方式。
掌握 Git,意味着你能够 高效管理代码、顺利参与团队协作、自信地处理复杂的开发场景。
"Git 是每个开发者的第二语言。" 🔀
—— 技术社区评价