Git 常用命令大全
目录
一、基础配置命令
1. 设置用户信息
1 2 3 4 5 6 7 8 9 10 11
| git config --global user.name "你的姓名"
git config --global user.email "你的邮箱@example.com"
git config --list
git config user.name
|
2. 其他配置
1 2 3 4 5 6 7 8
| git config --global core.editor "code --wait"
git config --global init.defaultBranch main
git config --global color.ui auto
|
返回目录
二、仓库创建与克隆
3. 初始化新仓库
1 2 3 4 5
| git init
git init <项目目录名>
|
4. 克隆现有仓库
1 2 3 4 5 6 7 8
| git clone <仓库URL>
git clone <仓库URL> <本地目录名>
git clone -b <分支名> <仓库URL>
|
返回目录
三、基本工作流程
5. 查看状态与差异
1 2 3 4 5 6 7 8 9 10 11
| git status
git status -s
git diff
git diff --staged
|
6. 添加文件到暂存区
1 2 3 4 5 6 7 8 9 10 11
| git add <文件名>
git add .
git add -u
git add -p
|
7. 提交更改
1 2 3 4 5 6 7 8
| git commit -m "提交说明"
git commit -am "提交说明"
git commit --amend
|
8. 文件操作
1 2 3 4 5 6 7 8
| git rm <文件名>
git rm --cached <文件名>
git mv <原文件名> <新文件名>
|
返回目录
四、分支管理
9. 分支操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git branch
git branch -a
git branch <分支名>
git checkout <分支名>
git checkout -b <分支名>
git branch -d <分支名>
git branch -D <分支名>
|
10. 合并与变基
1 2 3 4 5 6 7 8
| git merge <分支名>
git rebase <分支名>
git rebase -i <分支名或提交ID>
|
11. 储藏更改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git stash
git stash save "储藏说明"
git stash list
git stash apply
git stash apply stash@{0}
git stash pop
git stash clear
|
返回目录
五、远程仓库操作
12. 远程仓库管理
1 2 3 4 5 6 7 8 9 10 11
| git remote -v
git remote add <远程名> <仓库URL>
git remote set-url <远程名> <新URL>
git remote remove <远程名>
|
13. 推送与拉取
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git push <远程名> <分支名>
git push -u <远程名> <分支名>
git push -f
git pull <远程名> <分支名>
git fetch <远程名>
|
14. 远程分支操作
1 2 3 4 5 6 7 8
| git checkout --track <远程名>/<分支名>
git push <远程名> --delete <分支名>
git fetch -p
|
返回目录
六、撤销与回退
15. 撤销工作区修改
1 2 3 4 5 6 7 8
| git checkout -- <文件名>
git checkout .
git checkout <提交ID> -- <文件名>
|
16. 撤销暂存区修改
1 2 3 4 5
| git reset HEAD <文件名>
git reset HEAD
|
17. 回退提交
1 2 3 4 5 6 7 8 9 10 11
| git reset <提交ID>
git reset --hard <提交ID>
git reset --hard HEAD^
git reset --hard HEAD~3
|
18. 恢复提交
1 2 3 4 5
| git revert <提交ID>
git revert HEAD
|
返回目录
七、查看状态与历史
19. 查看提交历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git log
git log --oneline
git log --graph
git log -p <文件名>
git log --author="作者名"
git log -n 5
|
20. 查看文件更改
1 2 3 4 5 6 7 8 9 10 11
| git blame <文件名>
git log -p <文件名>
git diff
git diff --staged
|
返回目录
八、标签管理
21. 标签操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git tag
git tag <标签名>
git tag -a <标签名> -m "标签说明"
git show <标签名>
git push <远程名> <标签名>
git push <远程名> --tags
git tag -d <标签名>
git push <远程名> --delete <标签名>
|
返回目录
九、其他实用命令
22. 清理与维护
1 2 3 4 5 6 7 8 9
| git clean -n git clean -f
git gc
git fsck
|
23. 子模块
1 2 3 4 5 6 7 8 9 10 11
| git submodule add <仓库URL> <路径>
git submodule init
git submodule update
git clone --recurse-submodules <仓库URL>
|
24. 帮助信息
1 2 3 4 5 6 7 8
| git help <命令>
git <命令> -h
git help -a
|
25. 配置别名
1 2 3 4 5 6 7 8 9
| git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status
git co main git st
|
返回目录
实用技巧
常用工作流程示例
1 2 3 4 5 6
| git status git add . git commit -m "功能描述" git pull origin main git push origin feature
|
分支命名建议
main/master - 主分支
develop - 开发分支
feature/xxx - 功能分支
hotfix/xxx - 热修复分支
release/xxx - 发布分支
注意: 强制推送 (git push -f) 和硬重置 (git reset --hard) 会丢失数据,请谨慎使用!