git命令
【摘要】 Git 常用命令速查一、配置(首次使用)bashgit config --global user.name "你的名字"git config --global user.email "你的邮箱"# 查看配置git config --listgit config --global --edit # 编辑全局配置二、仓库初始化与克隆bashgit init ...
Git 常用命令速查
一、配置(首次使用)
bash
git config --global user.name "你的名字" git config --global user.email "你的邮箱" # 查看配置 git config --list git config --global --edit # 编辑全局配置
二、仓库初始化与克隆
bash
git init # 在当前目录初始化新仓库 git clone <url> # 克隆远程仓库 git clone <url> mydir # 克隆到指定目录 git clone -b <branch> <url> # 克隆指定分支
三、基本工作流
bash
git status # 查看工作区状态 git status -s # 简洁模式 git add <file> # 添加指定文件到暂存区 git add . # 添加所有改动 git add -p # 交互式选择要暂存的片段 git commit -m "说明" # 提交 git commit -am "说明" # 暂存已跟踪文件的改动并提交 git commit --amend # 修改最后一次提交(含信息) git commit --amend --no-edit # 只补充文件,不改提交信息
四、分支管理
bash
git branch # 列出本地分支 git branch -a # 列出所有分支(含远程) git branch <name> # 创建分支 git branch -d <name> # 删除已合并分支 git branch -D <name> # 强制删除分支 git branch -m <old> <new> # 重命名分支 git switch <name> # 切换分支(新版推荐) git checkout <name> # 切换分支(旧版) git switch -c <name> # 创建并切换 git checkout -b <name> # 创建并切换(旧版) git merge <branch> # 合并分支到当前分支 git merge --no-ff <branch> # 禁用快进,保留合并记录 git rebase <branch> # 变基 git cherry-pick <commit> # 摘取某次提交到当前分支
五、远程仓库
bash
git remote -v # 查看远程地址 git remote add origin <url> # 添加远程 git remote set-url origin <url> # 修改远程地址 git remote remove origin # 删除远程 git fetch # 拉取远程信息(不合并) git pull # 拉取并合并 git pull --rebase # 拉取并变基 git push # 推送 git push -u origin <branch> # 首次推送并建立关联 git push origin --delete <branch> # 删除远程分支 git push --tags # 推送标签
六、查看历史与差异
bash
git log # 提交历史 git log --oneline --graph --all # 图形化简洁历史 git log -p <file> # 某文件的修改历史 git log -n 5 # 最近 5 条 git log --author="名字" # 按作者过滤 git diff # 工作区 vs 暂存区 git diff --staged # 暂存区 vs 最近提交 git diff HEAD # 工作区 vs 最近提交 git diff <branch1> <branch2> # 两分支差异 git show <commit> # 查看某次提交详情 git blame <file> # 逐行查看谁改的
七、撤销与回退
bash
# 工作区 git restore <file> # 丢弃工作区改动 git checkout -- <file> # 同上(旧版) # 暂存区 git restore --staged <file> # 取消暂存 git reset HEAD <file> # 同上(旧版) # 提交历史 git reset --soft HEAD~1 # 撤销提交,保留改动在暂存区 git reset --mixed HEAD~1 # 撤销提交,保留改动在工作区(默认) git reset --hard HEAD~1 # 彻底撤销提交和改动(危险) git revert <commit> # 生成一个反向提交来抵消某次提交(安全,适用于已推送)
⚠️
reset --hard会丢失未提交的改动,慎用;已推送到远程的分支不要用reset,用revert。
八、暂存(stash)
bash
git stash # 临时保存当前改动 git stash -u # 包含未跟踪文件 git stash list # 查看 stash 列表 git stash pop # 恢复并删除最近一条 git stash apply # 恢复但保留记录 git stash drop # 删除最近一条 git stash clear # 清空所有
九、标签
bash
git tag # 列出标签 git tag v1.0.0 # 轻量标签 git tag -a v1.0.0 -m "说明" # 附注标签 git push origin v1.0.0 # 推送单个标签 git tag -d v1.0.0 # 删除本地标签 git push origin --delete v1.0.0 # 删除远程标签
十、常见场景速查
bash
# 提交信息写错了(还没推送) git commit --amend -m "新信息" # 漏加文件到最近一次提交 git add forgotten.txt git commit --amend --no-edit # 想改某个旧提交 → 交互式变基 git rebase -i HEAD~3 # 合并多个提交 git rebase -i HEAD~5 # 把 pick 改为 squash # 找哪个提交引入了 bug git bisect start git bisect bad git bisect good <commit> # ... 测试后 git bisect good/bad ... git bisect reset # 清理未跟踪文件(危险) git clean -n # 预览 git clean -fd # 强制删除未跟踪文件和目录 # 查看某文件在某次提交时的内容 git show <commit>:<file> # 从其他分支取某文件 git checkout <branch> -- <file>
十一、实用别名(可选,加到全局配置)
bash
git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.lg "log --oneline --graph --all --decorate"
之后就可以用 git lg 看漂亮的提交图了。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)