Git 使用指南
基础配置
安装 Git 后,首先配置用户名和邮箱:
bash
git config --global user.name "Your Name"
git config --global user.email "your@email.com"常用命令
仓库操作
bash
git init # 初始化仓库
git clone <url> # 克隆远程仓库
git remote add origin <url> # 关联远程仓库日常提交流程
bash
git status # 查看工作区状态
git add <file> # 暂存文件
git add . # 暂存所有修改
git commit -m "message" # 提交暂存区
git push origin main # 推送到远程
git pull origin main # 拉取远程更新分支管理
bash
git branch # 查看本地分支
git branch -a # 查看所有分支(含远程)
git branch <name> # 创建分支
git checkout <name> # 切换分支
git checkout -b <name> # 创建并切换分支
git merge <name> # 合并指定分支到当前分支
git branch -d <name> # 删除本地分支提交规范
推荐使用 Conventional Commits 规范:
feat: 新功能fix: 修复 Bugdocs: 文档更新style: 代码格式调整refactor: 重构test: 测试相关chore: 构建/工具配置
示例:
bash
git commit -m "feat: 添加用户登录功能"
git commit -m "fix: 修复首页加载异常"
git commit -m "docs: 更新 README"撤销操作
bash
git checkout -- <file> # 撤销工作区修改
git reset HEAD <file> # 取消暂存
git reset --soft HEAD~1 # 撤销 commit(保留修改)
git reset --hard HEAD~1 # 撤销 commit(丢弃修改)
git revert HEAD # 安全撤销(生成新 commit).gitignore
忽略不需要追踪的文件,例如:
node_modules/
dist/
.env
*.log