一、Git基础概念
1.1 Git是什么?
Git是一个分布式版本控制系统,由Linus Torvalds于2005年创建,用于管理Linux内核开发。与SVN等集中式版本控制系统不同,Git每个用户都有完整的仓库副本。
1.2 Git核心优势
- 分布式架构:每个开发者都有完整的项目历史记录
- 高效性能:大部分操作在本地完成,速度极快
- 强大的分支管理:分支创建、合并简单高效
- 数据完整性:所有内容在存储前都经过校验和计算
1.3 三个工作区域
- 工作目录(Working Directory):实际编辑文件的地方
- 暂存区(Staging Area/Index):准备提交的文件列表
- 仓库(Repository):存储项目所有版本和元数据
二、Git安装与配置
2.1 安装Git
1 2 3 4 5 6 7 8 9 10
| sudo apt-get install git
sudo yum install git
brew install git
|
2.2 基础配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git config --global user.name "你的名字" git config --global user.email "你的邮箱"
git config --global core.editor "vim"
git config --list
git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status git config --global alias.unstage 'reset HEAD --' git config --global alias.last 'log -1 HEAD'
|
2.3 配置验证
1 2 3 4 5 6
| git --version
git config user.name git config user.email
|
三、Git基础操作
3.1 创建仓库
1 2 3 4 5 6 7 8 9
| git init
git init my_project
git clone https://github.com/user/repo.git git clone https://github.com/user/repo.git my_folder
|
3.2 基础工作流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| git status git status -s
git add filename.txt git add . git add *.js git add src/
git commit -m "提交说明" git commit -m "fix: 修复登录bug" git commit -am "提交说明"
git log git log --oneline git log --graph git log -p git log --stat git log --since="2 weeks ago"
|
3.3 文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git mv oldname newname
git rm filename.txt git rm --cached filename.txt
node_modules/ *.log .DS_Store .env !important.log
|
四、分支管理
4.1 基础分支操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git branch git branch -a git branch -v
git branch feature-login git checkout -b feature-login
git checkout main git checkout feature-login
git checkout main git merge feature-login
git branch -d feature-login git branch -D feature-login
git branch -m old-name new-name
|
4.2 分支策略(Git Flow)
1 2 3 4 5 6 7 8 9 10
| main/master
develop
feature/* release/* hotfix/*
|
4.3 高级分支操作
1 2 3 4 5 6 7 8 9
| git log --oneline --graph --all
git merge --no-ff feature-login
git rebase main git rebase -i HEAD~3
|
五、远程仓库操作
5.1 管理远程仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git remote add origin https://github.com/user/repo.git
git remote -v
git remote rename origin upstream
git remote remove origin
git remote set-url origin https://github.com/user/new-repo.git
|
5.2 推送与拉取
1 2 3 4 5 6 7 8 9 10 11 12 13
| git push origin main git push origin feature-login git push -u origin feature-login git push --force origin main
git pull origin main git fetch origin git fetch --prune
git checkout -b feature-login origin/feature-login
|
5.3 协作工作流
六、撤销与回退
6.1 撤销工作区更改
1 2 3 4 5 6 7
| git checkout -- filename.txt git checkout -- .
git restore filename.txt git restore --staged filename.txt
|
6.2 撤销暂存区更改
1 2 3
| git reset HEAD filename.txt git restore --staged filename.txt
|
6.3 撤销提交
1 2 3 4 5 6 7 8 9 10 11
| git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1
git revert commit_hash
|
6.4 恢复已删除的文件
1 2 3 4 5
| git log --diff-filter=D --summary
git checkout commit_hash^ -- filename.txt
|
七、高级功能与技巧
7.1 储藏(Stash)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git stash git stash save "描述信息"
git stash list
git stash apply stash@{0} git stash pop
git stash drop stash@{0} git stash clear
|
7.2 标签管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git tag v1.0.0 git tag -a v1.0.0 -m "版本1.0.0"
git tag git show v1.0.0
git push origin v1.0.0 git push origin --tags
git tag -d v1.0.0 git push origin --delete v1.0.0
|
7.3 子模块(Submodule)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git submodule add https://github.com/user/repo.git libs/repo
git clone https://github.com/user/main-project.git git submodule init git submodule update
git submodule update --remote
|
八、问题排查与调试
8.1 查看差异
1 2 3 4 5 6 7 8 9 10 11
| git diff
git diff --staged
git diff commit1 commit2
git blame filename.txt
|
8.2 查找问题
1 2 3 4 5 6 7 8 9 10
| git bisect start git bisect bad git bisect good v1.0.0
git bisect reset
git log -S "function_name" git log --grep="关键字"
|
8.3 清理仓库
1 2 3 4 5 6 7 8 9
| git clean -n git clean -f git clean -fd git clean -xfd
git gc git prune
|
九、Git最佳实践
9.1 提交规范
推荐使用约定式提交(Conventional Commits):
feat: 新功能
fix: 修复bug
docs: 文档更新
style: 代码格式调整
refactor: 重构代码
test: 测试相关
chore: 构建过程或辅助工具变动
示例:
1 2 3
| git commit -m "feat: 添加用户登录功能" git commit -m "fix: 修复登录页面样式错位问题" git commit -m "docs: 更新API使用说明"
|
9.2 分支管理规范
- 主分支保护:main/master分支应设置保护,禁止直接推送
- 功能分支:每个功能/修复创建独立分支
- 及时删除:合并后及时删除已完成的分支
- 明确命名:使用清晰的命名规范,如
feature/user-auth、hotfix/payment-error
9.3 工作流程建议
- 频繁提交:小步提交,每次提交解决一个问题
- 先拉后推:推送前先拉取最新代码
- 解决冲突:在本地解决冲突后再推送
- 定期同步:每天开始工作前先同步远程仓库
十、常用命令速查表
| 类别 |
命令 |
说明 |
| 仓库 |
git init |
初始化仓库 |
|
git clone <url> |
克隆远程仓库 |
| 状态 |
git status |
查看状态 |
|
git log |
查看提交历史 |
| 操作 |
git add <file> |
添加到暂存区 |
|
git commit -m "msg" |
提交更改 |
|
git push |
推送到远程 |
|
git pull |
拉取并合并 |
| 分支 |
git branch |
查看分支 |
|
git checkout -b <name> |
创建并切换分支 |
|
git merge <branch> |
合并分支 |
| 撤销 |
git reset |
撤销提交 |
|
git revert |
创建撤销提交 |
|
git checkout -- <file> |
撤销工作区修改 |
| 其他 |
git stash |
储藏更改 |
|
git tag |
标签管理 |
|
git diff |
查看差异 |
总结
Git是一个强大而灵活的工具,掌握它需要理论学习和实践操作相结合。建议:
- 从基础开始:先掌握
add、commit、push、pull等基础命令
- 多实践:在实际项目中应用Git,遇到问题及时查阅文档
- 善用图形工具:如GitHub Desktop、SourceTree等辅助理解
- 学习工作流:根据团队需求选择合适的工作流程
记住,Git的核心思想是记录每次更改,而不是覆盖。通过合理使用分支、提交和合并,你可以高效地管理任何规模的项目。