日常Git命令梳理
【摘要】 一、配置相关# 设置用户名和邮箱git config --global user.name 'h*'git config --global user.email '*@163.com'# 查看所有全局配置git config --global --list# 查看/删除代理(容器环境)git config --global --list | grep proxygit config --g...
一、配置相关
# 设置用户名和邮箱
git config --global user.name 'h*'
git config --global user.email '*@163.com'
# 查看所有全局配置
git config --global --list
# 查看/删除代理(容器环境)
git config --global --list | grep proxy
git config --global --unset http.proxy
git config --global --unset https.proxy
# 存储凭证,避免每次输入密码/令牌
git config --global credential.helper store
二、克隆仓库
# 普通克隆
git clone https://gitcode.com/h*/ops-nn
# 只克隆单个分支,且只拉取最近一次提交(节省空间)
git clone --branch 0.4 --single-branch --depth 1 <仓库URL>
# 克隆时直接切换到指定标签(处于分离头指针状态)
git clone --depth 1 --branch v1.1-8.1.RC1.beta1 https://gitee.com/ascend/samples
三、分支操作
# 查看本地分支与远程分支的跟踪关系
git branch -vv
# 创建本地分支(不切换)
git branch sigmoid
# 创建并切换到新分支
git checkout -b sigmoid
# 切换到已有分支
git checkout sigmoid
# 删除本地分支(已合并)
git branch -d contrib4-new
# 删除远程分支
git push origin --delete contrib4-new
四、暂存区与工作区
# 查看状态
git status
# 查看未暂存的修改
git diff
# 查看已暂存的修改
git diff --cached
# 只添加已被跟踪的文件(不添加新文件)
git add -u
# 撤销所有暂存的文件(回到未暂存状态)
git reset HEAD .
# 丢弃所有未暂存的修改(慎重!仅针对已跟踪文件)
git checkout -- .
# 恢复被误删的文件(比回收站好用)
git restore add_custom
五、提交与修改
# 提交
git commit -m "message"
# 修改最近一次提交(追加文件或修改信息)
git add <漏掉的文件>
git commit --amend --no-edit # 不修改提交信息
git commit --amend -m "新信息" # 修改提交信息
# 注意:amend 会改变 commit hash,若已推送需强制推送
git push --force
# 更安全的强制推送
git push --force-with-lease
六、查看历史与提交详情
# 查看简洁日志(无日期)
git log --pretty=format:"%h %ad %s" --date=short
# 按提交信息搜索(支持正则)
git log --grep="qwen\|vl" -i --oneline
# 查看某个文件的提交历史(跟踪重命名)
git log --follow <file>
git log -p <file> # 带差异内容
# 查看某次提交的详细信息
git show <commit-id>
# 只查看文件列表
git show --name-only <commit-id>
# 查看统计(增删行数)
git show --stat <commit-id>
# 查看某次提交中特定文件的修改
git show <commit-id> -- build.sh
# 比较最近两次提交
git diff HEAD^ HEAD --stat
七、同步与合并
# 保持分支与 master 同步(rebase 方式)
git checkout your-branch
git fetch origin
git rebase origin/master
# 更新本地 master(添加 upstream 后)
git checkout master
git remote add upstream https://gitcode.com/cann/ops-nn
git fetch upstream
git merge upstream/master
git push
# 推送并设置上游分支
git push --set-upstream origin sigmoid
八、撤销与恢复
# 永久丢弃所有未提交的修改,恢复到指定提交(未跟踪文件不受影响)
git reset --hard <commit-id>
# 撤销所有暂存文件(同 git reset HEAD .)
git reset HEAD .
九、Stash(临时保存)
# 临时保存工作区修改
git stash
# 查看 stash 列表
git stash list
# 恢复最近的 stash
git stash pop
十、Cherry-Pick(精选提交)
# 将其他分支的特定提交移植到当前分支
git cherry-pick <commit-id1>
git cherry-pick <commit-id2>
十一、压缩提交(Squash)
# 将最近 3 个提交压缩成一个
git rebase -i HEAD~3
# 将后面的 pick 改为 squash(或 s)
# 压缩后强制推送
git push --force-with-lease origin sigmoid
十二、PR 相关
# 下载指定 PR 的代码到本地
git fetch origin merge-requests/106/head:pr-106
git checkout pr-303
十三、Patch 相关
# 生成最近一次提交的 patch 文件
git format-patch HEAD~1 -o patches
# 应用所有 patch
git am patches/*.patch
十四、标签
# 查看所有标签
git tag
# 切换到标签(分离头指针)
git checkout v0.4.0
十五、其他实用命令
# 查看某个文件在哪个 commit 被修改
git log --follow <file>
# 查看分支间差异的 commit 列表
git log --oneline master..contrib11
常见场景速查
| 场景 | 命令 |
|---|---|
| 提交后漏加文件 | git add file && git commit --amend --no-edit |
| 撤销所有暂存 | git reset HEAD . |
| 丢弃所有未暂存修改 | git checkout -- . |
| 恢复误删文件 | git restore <file> |
| 强制推送(安全) | git push --force-with-lease |
| 查看某次提交的详情 | git show <commit-id> |
| 临时保存工作区 | git stash |
| 压缩多个提交 | git rebase -i HEAD~N |
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)