Git速查命令

1. 状态检查

bash 复制代码
git status
git diff
git diff --cached
git log --oneline --graph --decorate --all
git branch -vv
git remote -v

常用顺序:

text 复制代码
先看状态
再看 diff
最后决定 add、commit、pull、push 或回退

2. 本地提交流程

bash 复制代码
git status
git diff
git add README.md
git diff --cached
git commit -m "docs(git): update note"

提交后推送:

bash 复制代码
git push

第一次推送新分支:

bash 复制代码
git push -u origin feature/xxx

3. 分支操作

bash 复制代码
git branch
git branch -a
git checkout -b feature/xxx
git switch -c feature/xxx
git checkout main
git switch main
git branch -d feature/xxx
git push origin --delete feature/xxx

查看分支和 upstream:

bash 复制代码
git branch -vv

4. 远程仓库

bash 复制代码
git remote -v
git remote add origin git@github.com:username/repo.git
git remote set-url origin git@github.com:username/repo.git
git fetch origin
git pull
git push

SSH 测试:

bash 复制代码
ssh -T git@github.com

5. merge、rebase、pull

合并功能分支:

bash 复制代码
git checkout main
git pull
git merge feature/xxx
git push

让功能分支跟上 main:

bash 复制代码
git checkout feature/xxx
git fetch origin
git rebase origin/main

pull 本质:

text 复制代码
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebase

6. 撤销和回退

撤销工作区修改:

bash 复制代码
git restore README.md
git restore .

取消暂存:

bash 复制代码
git restore --staged README.md

修改最近一次 commit:

bash 复制代码
git commit --amend -m "docs(git): update message"

回退本地提交:

bash 复制代码
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1

撤销已经公开的提交:

bash 复制代码
git revert <commit-hash>

清理未跟踪文件前先预览:

bash 复制代码
git clean -n
git clean -f

7. 冲突处理

merge 冲突:

bash 复制代码
git status
# 手动修改冲突文件
git add <file>
git commit

rebase 冲突:

bash 复制代码
git status
# 手动修改冲突文件
git add <file>
git rebase --continue

取消操作:

bash 复制代码
git merge --abort
git rebase --abort

8. .gitignore

常见规则:

gitignore 复制代码
build/
cmake-build-*/
*.o
*.log
.env
.vscode/
.idea/

已经被 Git 跟踪的文件,单独写入 .gitignore 不会自动失效。

需要:

bash 复制代码
git rm --cached path/to/file

排查忽略规则:

bash 复制代码
git check-ignore -v path/to/file

9. commit message

推荐格式:

text 复制代码
<type>(<scope>): <summary>

常见例子:

text 复制代码
feat(redis): add RESP array parser
fix(http): handle keep-alive timeout
docs(git): add rollback note
refactor(net): simplify event loop cleanup
test(parser): cover invalid bulk string
chore(cmake): enable compile warnings

10. 常见错误入口

text 复制代码
not a git repository
当前目录不是 Git 仓库,先 pwd 和 ls -la

remote origin already exists
origin 已存在,用 git remote -v 查看,用 set-url 修改

Permission denied (publickey)
SSH 没配好或权限不对,先 ssh -T git@github.com

failed to push some refs / non-fast-forward
远程有本地没有的提交,先 fetch,再 merge 或 rebase

Your local changes would be overwritten
工作区有未提交修改,先 commit、stash 或 restore

detached HEAD
当前站在 commit 上,不在分支上,需要保留就 switch -c 新分支

11. 排查命令

bash 复制代码
pwd
git status
git branch -vv
git remote -v
git log --oneline --graph --decorate --all

先看清楚:

text 复制代码
在哪个目录
在哪个分支
工作区是否干净
远程地址是否正确
本地和远程历史是否一致
相关推荐
cxwl3sxl1 小时前
重装系统后GIT仓库修复
git
OYangxf1 小时前
Git Common Errors
大数据·git·elasticsearch
189228048611 小时前
NV236美光MT29F32T08GWLBHD6-24TES:B
大数据·服务器·人工智能·科技·缓存
敖正炀1 小时前
映射与文档建模:动态映射、嵌套与父子关系
elasticsearch
xiaogutou11211 小时前
从2小时到5分钟:超市促销海报的AI生成方案
大数据·人工智能
Mike_6661 小时前
git@gitlab-rdc.xxxxx.com: Permission denied (publickey).fatal: 无法读取远程仓库。
git·elasticsearch·gitlab
明月_清风1 小时前
深入浅出 Elasticsearch:核心概念、工具链与底层原理全解析
后端·elasticsearch
hughnz2 小时前
下一代地热能的技术障碍
java·大数据·数据库
极光代码工作室2 小时前
基于大数据的交通流量分析系统
大数据·hadoop·python·数据分析·数据可视化