git常用命令集
1720 字
9 分钟
git常用命令集
Git 常用命令完整版
覆盖日常开发 95% 以上场景,包含基础操作、团队协作、冲突处理、历史整理、误操作恢复等。
目录
- 配置
- 创建与克隆仓库
- 日常提交
- 查看差异与历史
- 分支操作
- 远程仓库
- 撤销与回退
- 标签
- 暂存工作区 stash
- 清理工作区
- 子模块
- .gitignore 相关
- 常见工作流
- 冲突解决
- 高级操作
- 安全与恢复
- 帮助命令
1. 配置
bash
# 设置用户名和邮箱git config --global user.name "Your Name"git config --global user.email "you@example.com"
# 查看配置git config --listgit config --global --listgit config --local --list
# 设置默认编辑器git config --global core.editor "vim"
# 设置别名git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commitgit config --global alias.lg "log --oneline --graph --decorate --all"2. 创建与克隆仓库
bash
# 初始化本地仓库git init
# 克隆远程仓库git clone <url>
# 克隆指定分支git clone -b <branch> <url>
# 浅克隆(只拉取最近一次提交)git clone --depth 1 <url>3. 日常提交
bash
# 查看状态git statusgit status -s
# 添加到暂存区git add <file>git add .git add -Agit add -p # 交互式选择部分修改
# 提交git commit -m "message"git commit -am "message" # 仅对已跟踪文件有效git commit --amend # 修改上一次提交git commit --amend --no-edit # 修改上一次提交但不改提交信息4. 查看差异与历史
bash
# 查看差异git diffgit diff --stagedgit diff <branch1>..<branch2>git diff <commit1> <commit2>
# 查看历史git loggit log --onelinegit log --oneline --graph --decorate --allgit log -p <file>git show <commit>git blame <file>git blame -L 10,20 <file> # 查看指定行范围
# 搜索提交git log -S "text" # 查找添加/删除该字符串的提交git log -G "regex" # 查找匹配正则的提交
# 搜索代码git grep "keyword"
# 查看引用日志git reflog5. 分支操作
bash
# 查看分支git branchgit branch -agit branch -vv
# 创建分支git branch <name>
# 切换分支git switch <branch>git checkout <branch>git switch - # 切换回上一个分支git checkout - # 旧写法
# 创建并切换分支git switch -c <new-branch>git checkout -b <new-branch>
# 删除分支git branch -d <branch>git branch -D <branch> # 强制删除
# 重命名分支git branch -m <old> <new>git branch -M main # 强制重命名当前分支为 main
# 合并git merge <branch>git merge --no-ff <branch>
# 变基git rebase <branch>git rebase -i HEAD~3
# 拣选提交git cherry-pick <commit>6. 远程仓库
bash
# 查看远程git remote -vgit remote show origin
# 添加远程git remote add origin <url>
# 修改远程地址git remote set-url origin <url>
# 删除远程git remote remove origin
# 拉取git fetchgit fetch --all --prunegit pullgit pull --rebase
# 推送git pushgit push -u origin <branch>git push origin <branch>
# 安全强推(推荐)git push --force-with-lease
# 强制推送(慎用)git push --force
# 删除远程分支git push origin --delete <branch>
# 推送标签git push --tags7. 撤销与回退
bash
# 丢弃工作区修改git restore <file>git checkout -- <file> # 旧写法
# 取消暂存git restore --staged <file>git reset HEAD <file> # 旧写法
# 从指定提交恢复文件git restore --source=<commit> <file>
# 回退提交git reset --soft HEAD~1 # 保留修改,保留暂存git reset --mixed HEAD~1 # 保留修改,取消暂存(默认)git reset --hard HEAD~1 # 丢弃修改,慎用
# 反做某次提交git revert <commit>8. 标签
bash
# 查看标签git tag
# 创建附注标签git tag -a v1.0.0 -m "release v1.0.0"
# 查看标签详情git show v1.0.0
# 推送标签git push origin v1.0.0git push origin --tags
# 删除本地标签git tag -d v1.0.0
# 删除远程标签git push origin --delete tag v1.0.09. 暂存工作区 stash
bash
# 保存当前修改git stashgit stash push -m "message"git stash save "message"
# 查看 stashgit stash listgit stash show
# 恢复git stash applygit stash pop
# 删除git stash drop stash@{0}git stash clear10. 清理工作区
bash
# 预览将要删除的未跟踪文件git clean -n
# 删除未跟踪文件git clean -f
# 删除未跟踪文件和目录git clean -fd
# 包括 .gitignore 忽略的文件(慎用)git clean -fdx
git clean -fdx和git reset --hard会丢失数据,慎用哈
11. 子模块
bash
# 添加子模块git submodule add <url> <path>
# 查看子模块git submodule status
# 初始化并更新子模块git submodule update --init --recursive
# 更新子模块到远程最新git submodule update --remote12. .gitignore 相关
bash
# 创建 .gitignoreecho "node_modules/" >> .gitignoreecho "dist/" >> .gitignore
# 停止跟踪已提交文件,但保留本地文件git rm -r --cached <file>git rm -r --cached .
git add .git commit -m "update .gitignore"13. 常见工作流
初始化并推送到远程
bash
git initgit add .git commit -m "init"git branch -M maingit remote add origin <url>git push -u origin main同步远程主分支
bash
git fetch --all --prunegit switch maingit pull --rebase合并远程主分支到当前分支
bash
git fetch origingit merge origin/main
# 或git rebase origin/main临时保存当前修改
bash
git stash push -m "wip"git switch maingit pullgit switch -git stash pop安全强推
bash
git push --force-with-lease14. 冲突解决
合并冲突
bash
git status # 查看冲突文件# 手动编辑冲突文件,保留需要的代码git add <file>git merge --continue# 或git commit变基冲突
bash
git status# 手动编辑冲突文件git add <file>git rebase --continue# 跳过当前提交git rebase --skip# 中止变基git rebase --abort拣选冲突
bash
git status# 手动编辑冲突文件git add <file>git cherry-pick --continue# 中止git cherry-pick --abort中止合并
bash
git merge --abort15. 高级操作
交互式 rebase
bash
git rebase -i HEAD~n# 常用操作:# pick 保留提交# reword 修改提交信息# edit 修改提交内容# squash 合并到上一个提交并保留信息# fixup 合并到上一个提交并丢弃信息# drop 删除提交修复旧提交
bash
git commit --fixup <commit>git rebase -i --autosquash <commit>~1二分查找问题提交
bash
git bisect startgit bisect bad # 当前版本有问题git bisect good <commit> # 已知正常版本# Git 会自动切换到中间提交,测试后标记git bisect goodgit bisect bad# 找到后重置git bisect reset多工作区
bash
git worktree add ../hotfix maingit worktree listgit worktree remove ../hotfix补丁协作
bash
# 生成补丁git format-patch -1 <commit>git format-patch <commit1>..<commit2>
# 应用补丁git am < patch-filegit apply < patch-file大文件 LFS
bash
git lfs installgit lfs track "*.psd"git add .gitattributes仓库维护
bash
git gcgit fsckgit count-objects -vH钩子与规范
bash
# 本地钩子目录.git/hooks
# 常用工具pre-commithuskycommitlint16. 安全与恢复
bash
# 查看所有操作记录git reflog
# 恢复到某个操作之前git reset --hard HEAD@{1}
# 为某次提交创建救援分支git branch rescue <commit>
# 安全强推git push --force-with-lease误操作别着急,先
git reflog,大多数提交都能找回
17. 帮助命令
bash
git help <command>git <command> -h慎用命令
git reset --hardgit clean -fdxgit push --forcegit rebase已推送到远端的公共分支
建议:提交前先
git status,推送前先git pull --rebase,强推优先用--force-with-lease
文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!