主题
Git 日常使用指南
首次配置
bash
git config --global user.name "你的名字"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.autocrlf input # Windows 用户可设为 true基本工作流
bash
git clone https://github.com/user/repo.git
git status # 查看改动
git add . # 暂存全部改动
git commit -m "feat: 新增图床文章"
git pull --rebase # 拉取远端并变基,保持历史整洁
git push提交信息规范
推荐使用 Conventional Commits:
| 前缀 | 含义 |
|---|---|
feat | 新功能 / 新文章 |
fix | 修复问题 |
docs | 文档修改 |
style | 样式调整(不影响逻辑) |
refactor | 重构 |
chore | 构建、依赖等杂项 |
分支协作
bash
git switch -c feature/footer # 新建并切换分支
git switch main
git merge feature/footer
git branch -d feature/footer # 删除已合并分支后悔药
| 场景 | 命令 |
|---|---|
| 撤销工作区某文件的修改 | git restore file.md |
| 取消暂存 | git restore --staged file.md |
| 修改最近一次提交信息 | git commit --amend |
| 撤回最近一次提交但保留改动 | git reset --soft HEAD~1 |
| 生成一个反向提交(已推送时用) | git revert <commit> |
| 找回误删的提交 | git reflog 找到哈希后 git reset --hard <hash> |
WARNING
已经推送到远端的提交,尽量用 revert 而不是 reset + 强推,避免影响他人。
.gitignore 示例(VitePress 项目)
txt
node_modules/
docs/.vitepress/dist/
docs/.vitepress/cache/
.DS_Store