什么是Git?
Git是分布式版本控制系统,记录代码的每次改动,可以随时回退。
简单理解:
- 写代码→提交到Git→记录历史
- 改错了→回退到之前的版本
- 团队协作→拉取代码→修改→推送
安装Git
1 2 3 4 5 6 7 8
| apt install -y git
yum install -y git
git --version
|
配置Git
1 2 3 4 5 6 7 8
| git config --global user.name "你的名字"
git config --global user.email "your@example.com"
git config --list
|
基本概念
- 工作区:你修改代码的地方
- 暂存区:
git add 后的文件
- 本地仓库:
git commit 后的记录
- 远程仓库:GitHub/GitLab上的仓库
创建和克隆仓库
创建新仓库
1 2 3 4 5
| cd /path/to/project
git init
|
克隆远程仓库
1 2
| git clone https://github.com/username/repository.git git clone git@github.com:username/repository.git
|
基本工作流程
1. 查看状态
2. 添加到暂存区
1 2 3 4 5 6 7 8 9
| git add file.txt
git add . git add -A
git add *.txt
|
3. 提交到本地仓库
1 2
| git commit -m "提交信息" git commit -am "提交信息"
|
提交信息规范:
1 2 3 4 5 6
| feat: 添加新功能 fix: 修复bug docs: 更新文档 style: 修改格式 refactor: 重构代码 test: 添加测试
|
4. 推送到远程仓库
1 2 3 4 5
| git push -u origin master
git push
|
查看历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git log
git log --oneline
git log --graph --oneline
git log -p
git log -n 5
|
分支管理
查看分支
1 2 3
| git branch git branch -r git branch -a
|
创建分支
切换分支
1 2 3
| git checkout feature-x
git switch feature-x
|
创建并切换分支
1 2 3
| git checkout -b feature-x
git switch -c feature-x
|
删除分支
1 2 3 4 5 6 7 8
| git branch -d feature-x
git branch -D feature-x
git push origin --delete feature-x
|
合并分支
1 2 3 4 5 6 7 8
| git checkout master
git merge feature-x
git branch -d feature-x
|
解决冲突
当合并出现冲突时,手动编辑冲突文件:
1 2 3 4 5
| <<<<<<< HEAD 你的代码 ======= 其他人的代码 >>>>>>> feature-x
|
解决后:
1 2
| git add . git commit -m "解决冲突"
|
忽略文件
创建 .gitignore 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| config.ini secrets.txt
node_modules/ __pycache__/ .vscode/
*.log *.tmp
!important.log
|
常用命令
撤销操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git checkout file.txt git restore file.txt
git reset HEAD file.txt git restore --staged file.txt
git reset --soft HEAD~1
git reset --hard HEAD~1
git reset --hard 提交哈希
|
查看差异
1 2 3 4 5 6 7 8
| git diff
git diff --cached
git diff HEAD~2 HEAD
|
暂存工作
1 2 3 4 5 6 7 8 9 10 11
| git stash
git stash list
git stash pop
git stash drop
|
远程仓库操作
添加远程仓库
1
| git remote add origin https://github.com/username/repo.git
|
查看远程仓库
拉取远程更新
推送到远程
1 2
| git push origin master git push origin feature-x
|
标签管理
创建标签
1 2
| git tag v1.0.0 git tag -a v1.0.0 -m "版本1.0.0"
|
查看标签
推送标签
1 2
| git push origin v1.0.0 git push origin --tags
|
删除标签
1 2
| git tag -d v1.0.0 git push origin --delete v1.0.0
|
实战场景
场景1:开发新功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git checkout -b feature-login
git add . git commit -m "feat: 添加登录功能"
git push -u origin feature-login
git checkout master git merge feature-login git push origin master
|
场景2:修复紧急Bug
1 2 3 4 5 6 7 8 9 10 11 12 13
| git checkout -b hotfix-bug
git add . git commit -m "fix: 修复紧急bug"
git checkout master git merge hotfix-bug git checkout develop git merge hotfix-bug
|
场景3:误删文件恢复
1 2 3 4 5
| git log --all --full-history -- 文件名
git checkout 提交哈希 -- 文件名
|
高级技巧
技巧1:修改最后一次提交
1 2 3 4 5 6
| git commit --amend -m "新的提交信息"
git add 忘记的文件 git commit --amend
|
技巧2:选择性合并
技巧3:重命名分支
1
| git branch -m old-name new-name
|
技巧4:查看文件历史
1
| git log --follow file.txt
|
常见问题
Q: push时提示冲突?
A:
1 2 3 4
| git pull --rebase origin master
git push
|
Q: 查看某次提交的详细内容?
A:
Q: 恢复误删的分支?
A:
1 2 3
| git reflog git checkout 提交哈希 git checkout -b 分支名
|
Q: 清理未跟踪的文件?
A:
总结
- 初始化:
git init,克隆:git clone
- 工作流:
git add→git commit→git push
- 查看状态:
git status,历史:git log
- 分支:
git branch 创建,git checkout 切换,git merge 合并
- 撤销:
git checkout 修改工作区,git reset 撤销提交
- 远程:
git pull 拉取,git push 推送
.gitignore 忽略文件,git stash 暂存工作
- 冲突解决:手动编辑文件后
git add + git commit
下一篇:压缩解压全攻略。