一、Git 核心概念
1. 三个工作区域
git add git commit git push git pull 工作目录
Working Directory 暂存区
Staging Area 本地仓库
Local Repository 远程仓库
Remote Repository
2. 文件生命周期
新文件 git add git commit 编辑文件 git add git add git rm Untracked Staged Unmodified Modified
二、Git 安装与配置
1. 全局配置
bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "code --wait" # 使用VS Code作为编辑器
git config --global init.defaultBranch main # 设置默认分支
2. 常用配置项
命令 | 描述 |
---|---|
git config --list |
查看所有配置 |
git config --global alias.co checkout |
创建别名 |
git config --global core.autocrlf input |
Linux/Mac换行符设置 |
git config --global core.autocrlf true |
Windows换行符设置 |
三、基础工作流
1. 仓库操作
bash
# 初始化新仓库
git init
# 克隆远程仓库
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git myfolder # 指定目录
# 查看仓库状态
git status
git status -s # 精简输出
2. 文件操作
bash
# 添加文件到暂存区
git add filename.txt # 添加单个文件
git add . # 添加所有修改
git add *.js # 添加所有js文件
# 提交更改
git commit -m "描述信息"
git commit -a -m "跳过暂存区直接提交" # 自动添加跟踪文件
# 查看提交历史
git log
git log --oneline # 单行显示
git log --graph # 图形化显示分支
git log -p filename.txt # 查看文件变更历史
四、分支管理
1. 分支操作
bash
# 创建与切换分支
git branch feature-login # 创建分支
git checkout feature-login # 切换分支
git checkout -b hotfix-123 # 创建并切换
# 合并分支
git checkout main
git merge feature-login # 合并到当前分支
# 删除分支
git branch -d feature-login # 安全删除
git branch -D feature-login # 强制删除
# 查看分支
git branch # 本地分支
git branch -a # 所有分支(含远程)
git branch -v # 查看分支最后提交
五、远程仓库
1. 远程操作
bash
# 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 查看远程仓库
git remote -v
# 推送分支
git push -u origin main # 首次推送设置上游
git push # 后续推送
# 拉取更新
git pull # 拉取并合并
git fetch # 仅获取不合并
# 跟踪远程分支
git branch --set-upstream-to=origin/main main
2. 多人协作流程
bash
# 1. 获取最新代码
git fetch origin
# 2. 合并到本地分支
git merge origin/main
# 3. 创建特性分支
git checkout -b feature-search
# 4. 开发并提交
git add .
git commit -m "添加搜索功能"
# 5. 推送到远程
git push -u origin feature-search
# 6. 创建Pull Request
六、高级操作
1. 撤销更改
bash
# 撤销工作区修改
git checkout -- filename.txt
# 撤销暂存区文件
git reset HEAD filename.txt
# 撤销最近提交
git reset --soft HEAD~1 # 保留更改在暂存区
git reset --hard HEAD~1 # 丢弃更改
# 修改最后一次提交
git commit --amend
2. 储藏与标签
bash
# 储藏当前工作
git stash # 储藏修改
git stash list # 查看储藏列表
git stash apply # 应用最近储藏
git stash drop # 删除最近储藏
# 标签管理
git tag v1.0.0 # 创建轻量标签
git tag -a v1.1.0 -m "发布版本1.1.0" # 附注标签
git push origin --tags # 推送所有标签
3. 变基操作
bash
# 变基整合提交历史
git checkout feature
git rebase main
# 交互式变基(修改历史)
git rebase -i HEAD~3
# 操作选项:
# p, pick = 使用提交
# r, reword = 修改提交信息
# e, edit = 修改提交内容
# s, squash = 合并到前一个提交
# d, drop = 删除提交
七、常见问题解决
1. 冲突解决
bash
# 发生冲突时
<<<<<<< HEAD
本地修改内容
=======
远程修改内容
>>>>>>> branch-name
# 解决步骤:
1. 编辑文件,删除冲突标记
2. git add filename.txt
3. git commit -m "解决冲突"
2. 恢复删除文件
bash
# 查看删除记录
git log --diff-filter=D -- path/to/file
# 恢复文件
git checkout <commit-hash>^ -- path/to/file
3. 清理历史
bash
# 从历史中移除敏感文件
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch path/to/file" \
--prune-empty --tag-name-filter cat -- --all
八、最佳实践
-
提交规范:
-
使用约定式提交
feat: 添加用户登录功能
fix: 修复支付接口错误
docs: 更新API文档
-
-
分支命名:
- feature/功能名称
- bugfix/问题描述
- hotfix/紧急修复
-
.gitignore 示例:
gitignore# 忽略日志文件 *.log # 忽略编译产物 /build/ /dist/ # 忽略环境文件 .env # 忽略IDE文件 .idea/ .vscode/
-
日常习惯:
- 频繁提交小变更
- 提交前运行测试
- 拉取前先储藏本地修改
- 定期清理远程已合并分支
九、可视化工具
工具 | 平台 | 特点 |
---|---|---|
GitKraken | 全平台 | 强大的分支可视化 |
SourceTree | Win/Mac | 免费专业版 |
GitHub Desktop | Win/Mac | 简洁易用 |
GitLens | VS Code扩展 | IDE集成 |
小贴士:掌握命令行操作后使用可视化工具效率更高
本手册涵盖Git核心功能,建议结合实践操作加深理解。遇到问题时,使用git help <command>
查看官方文档!
