我来为您整理一份全面的 Git 指令集,涵盖从基础到高级的各种操作场景。
🚀 Git 指令集大全
一、基础配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
git config --list git config user.name
git config --global core.editor "vim"
git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit
|
二、仓库操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git init
git clone <repository-url> git clone <repository-url> <directory-name>
git remote -v
git remote add origin <repository-url>
git remote set-url origin <new-url>
|
三、基本工作流程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git status
git add <file-name> git add . git add -A
git commit -m "提交信息" git commit -am "提交信息"
git log git log --oneline git log --graph --oneline git log -n 5
|
四、分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git branch git branch -a git branch -r
git branch <branch-name> git checkout -b <branch-name> git switch -c <branch-name>
git checkout <branch-name> git switch <branch-name>
git merge <branch-name>
git branch -d <branch-name> git branch -D <branch-name>
git branch -m <old-name> <new-name>
|
五、远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git pull origin <branch> git pull --rebase origin <branch>
git push origin <branch> git push -u origin <branch> git push origin --delete <branch>
git fetch origin
git remote prune origin
|
六、撤销与回退
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git checkout -- <file> git restore <file>
git reset HEAD <file> git restore --staged <file>
git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1
git reflog
|
七、储藏(Stash)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git stash git stash save "描述信息"
git stash list
git stash pop git stash apply git stash apply stash@{0}
git stash drop stash@{0} git stash clear
|
八、标签管理
1 2 3 4 5 6 7 8 9 10 11 12
| git tag <tag-name> git tag -a <tag-name> -m "描述" git tag <tag-name> <commit-id>
git push origin <tag-name> git push origin --tags
git tag -d <tag-name> git push origin :refs/tags/<tag-name>
|
九、高级操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git rebase <branch> git rebase -i HEAD~3
git cherry-pick <commit-id>
git bisect start git bisect bad git bisect good <commit-id>
git blame <file>
git blame -L 10,20 <file>
|
十、常用组合场景
| 场景 | 命令组合 |
| 日常提交 | git add . → git commit -m "msg" → git push |
| 拉取最新代码 | git pull origin main |
| 创建功能分支 | git checkout -b feature/xxx → 开发 → git push -u origin feature/xxx |
| 合并功能分支 | git checkout main → git pull → git merge feature/xxx → git push |
| 临时保存工作 | git stash → 切换分支处理 → git checkout - → git stash pop |
| 撤销错误提交 | git reset --soft HEAD~1 → 修改后重新提交 |
十一、.gitignore 模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # 依赖 node_modules/ vendor/
# 编译输出 dist/ build/ *.exe *.dll
# IDE .idea/ .vscode/ *.iml
# 日志与临时文件 *.log *.tmp .DS_Store
# 敏感信息 .env config.local.php
|
十二、实用技巧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git log -p <file>
git diff branch1 branch2
git diff
git diff --cached
git diff --name-only
git shortlog -sn
git format-patch HEAD~3
git apply patch-file
|
如需了解某个指令的详细用法,可以使用 git <command> --help 查看官方文档。