附录C:Git命令
配置默认编辑器
git config --global core.editor "xxx"
配置提交时的用户名称和邮箱
git config --global user.name "xxx"
git config --global user.email xxx.com
配置Git命令的别名
git config --global alias.xx 'xxx'
新建一个仓库
git init
克隆一个仓库
git clone [-o [remoteName]] <url>
从工作目录到暂存区
git add [./file]
查看状态
git status
查看任意两棵树的差异
- 工作区和暂存区的差异
git diff
- 暂存区和最后提交的差异
git diff --staged
- 两个提交之间的差异
git diff <branch> <branch>
- 查看分支之间的某段差异
git diff A..B
// 大于A小于B,即存在于B但不在A中的差异
提交
- 简单提交
git commit -m ‘xxx’
- 跳过add提交
git commit -a -m 'aaa'
- 重做提交
git commit --amend
重置
- 重置HEAD
git reset --soft <commit hash>
- 重置HEAD和暂存区
git reset --mixed <commit hash>
- 全部重置
git reset --hard <commit hash>
分支
- 创建一个新分支
git branch <branch>
- 分支查看
git branch [-v] [-vv]
- 分支删除
git branch -d <branch>
- 查看和当前分支有没有合并的分支
git branch [--merged] [--no-merged]
- 指定跟踪分支
git branch -u <remote>/<branch>
分支检出
- 分支切换
git checkout <branch>
- 创建分支并切换
git checkout -b <branch>
- 创建或切换分支并指定跟踪分支
git checkout -b <branch> <remote>/<branch>
合并
- 分支合并
git merge <branch>
- 单独合并某一个提交的差异到当前分支
git cherry-pick <commit hash>
提交历史
- 查看历史,默认从当前分支的最近提交开始
git log
- 限制查看数量
git log -<num>
- 查看每次提交所引入的差异
git log -p
- 限制格式化
git log
git log
- 关键字匹配
git log
贮藏
- 贮藏暂存区和工作目录
git stash
- 查看所有贮藏
git stash list
- 应用贮藏
git stash apply
- 指定应用贮藏
git stash apply stash@{num}
- 暂存区恢复
git stash apply --index
- 删除贮藏
git stash drop stash@{num}
- 应用并丢弃
git stash pop
- 从贮藏创建分支
git stash branch <branch>
拉取
- 抓取
git fetch <remote>
拉取并合并
- 拉取合并
git pull
推送
- 推送
git push <remote> <branch>
远程仓库管理
- 查看远程仓库
git remote [-v]
- 添加远程仓库
git remote add <name> <url>
- 查看某个远程仓库
git remote show <remote>
- 重命名远程仓库的本地记号
git remote rename <old> <new>
- 删除远程仓库
git remote remove <remote>
变基
- 变基合并
git rebase <base> <topic>
- 三分支变基
- 取出b3分支找出它与b2不同的补丁,然后将这些补丁变基在b1上
git rebase --onto <b1> <b2> <b3>
数据恢复
- 查找丢失的commit
git reflog
git log -g
git fsck --full
- 分支指向一个commit
git branch <branch> <commit>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?