Skip to content

Zhaotizi 编辑部 · 更新于

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