记录一些 Git 日常高频操作,方便查阅。
基础操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git init
git clone <url>
git status
git add .
git commit -m "feat: add new feature"
git push origin main
|
分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git checkout -b feature/my-feature
git branch -a
git merge feature/my-feature
git branch -d feature/my-feature
git push origin --delete feature/my-feature
|
撤销操作
1 2 3 4 5 6 7 8 9 10 11
| git checkout -- <file>
git reset HEAD <file>
git reset --soft HEAD~1
git reset --hard HEAD~1
|
查看历史
1 2 3 4 5 6 7 8
| git log --oneline --graph --all
git log -p <file>
git show <commit-hash>
|
暂存工作区(stash)
1 2 3 4 5 6 7 8 9 10 11
| git stash
git stash list
git stash pop
git stash apply stash@{2}
|
远程仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git remote -v
git remote add origin <url>
git fetch origin
git pull origin main
git push --force-with-lease origin main
|
Commit 规范
推荐使用 Conventional Commits:
| 前缀 |
含义 |
feat |
新功能 |
fix |
Bug 修复 |
docs |
文档更新 |
style |
代码格式(不影响逻辑) |
refactor |
重构 |
chore |
构建/工具链相关 |
示例:git commit -m "fix: resolve login redirect issue"