Beginner's Guide to Git

5 min read
Table of contents
- 常用
- 0. remote
- 1. git status / add / commit / restore / reset
- 2. git log / diff / show
- 3. git checkout / switch
- 4. git merge / rebase / cherry-pick
- 5. git fetch / pull / push
- 6. git tag
- 7. git stash
- 8. git config
- 9. git clean
- 10. git blame / reflog / bisect / submodule
- 11. git archive / svn / gc / fsck
- 12. help & 其他
常用
### -1. branch
git branch # 列出本地分支
git branch -a # 列出所有分支(本地+远程)
git branch newbranch # 创建新分支
git branch -d dev # 删除分支
git branch -m old new # 重命名分支
git branch --merged # 列出已合并分支
git branch --no-merged # 列出未合并分支
git branch -vv # 查看分支和跟踪信息
git branch -u origin/dev # 设置上游分支
git branch --unset-upstream # 取消上游
git branch --show-current # 显示当前分支名
git branch --contains <commit> # 哪些分支包含某提交
git branch --list "feature/*" # 按模式列出分支
0. remote
git remote # 列出远程
git remote -v # 列出远程和 URL
git remote add origin https://xx # 添加远程
git remote remove origin # 删除远程
git remote rename origin upstream # 重命名远程
git remote set-url origin https://yy # 修改 URL
git remote get-url origin # 显示 URL
git remote show origin # 显示详细信息
git remote prune origin # 清理失效远程分支
git remote update # 拉取所有远程更新
git remote set-head origin main # 设置远程 HEAD
1. git status / add / commit / restore / reset
git status # 查看当前工作区状态
git status -s # 简洁显示状态
git add <file> # 暂存文件
git add . # 暂存所有更改
git add -u # 暂存已跟踪文件的修改和删除
git add -p # 按hunk交互式暂存
git commit -m "msg" # 提交暂存区
git commit --amend # 修改最后一次提交
git commit --amend --no-edit # 只修正内容,不改提交说明
git restore <file> # 撤销工作区修改(未add)
git restore --staged <file> # 撤销add(回到未暂存状态)
git reset <file> # 撤销add(回到未暂存状态)
git reset --hard # 重置工作区和暂存区到HEAD
git reset --soft HEAD~1 # 回退1次提交,保留改动到暂存区
git reset --mixed HEAD~1 # 回退1次提交,改动到未暂存区
git reset --hard HEAD~1 # 丢弃本地所有改动,强制回退
2. git log / diff / show
git log # 查看提交历史
git log --oneline # 单行显示
git log --graph --all --decorate # 图形化显示所有分支
git diff # 比较工作区和暂存区
git diff --cached # 比较暂存区和HEAD
git diff <branch1>..<branch2> # 比较两个分支
git diff <commit> # 查看指定提交与工作区的差异
git show <commit> # 查看某次提交内容
git show HEAD # 查看最新提交
3. git checkout / switch
git checkout <branch> # 切换分支
git checkout -b <newbranch> # 新建并切换分支
git checkout <file> # 撤销文件的更改
git switch <branch> # 新版推荐:切换分支
git switch -c <newbranch> # 新建并切换分支
4. git merge / rebase / cherry-pick
git merge <branch> # 合并分支
git merge --no-ff <branch> # 保留合并记录
git merge --squash <branch> # 压缩合并
git rebase <branch> # 变基到指定分支
git rebase -i HEAD~n # 交互式变基(如合并多次提交)
git rebase --abort # 放弃rebase
git rebase --continue # 继续rebase
git cherry-pick <commit> # 应用指定提交到当前分支
5. git fetch / pull / push
git fetch # 拉取远程更新,不合并
git fetch origin # 拉取origin仓库
git pull # 拉取并合并
git pull --rebase # 拉取并变基
git push # 推送当前分支到远程
git push origin <branch> # 推送指定分支
git push -u origin <branch> # 推送并设置上游
git push --delete origin <branch> # 删除远程分支
git push --tags # 推送所有标签
6. git tag
git tag # 列出标签
git tag <name> # 新建标签
git tag -a <name> -m "msg" # 新建带注释的标签
git tag -d <name> # 删除本地标签
git push origin <name> # 推送标签到远程
git push origin --tags # 推送所有标签
git tag -l 'v1.*' # 按模式列出标签
7. git stash
git stash # 保存当前工作进度
git stash pop # 恢复最近一次stash并删除
git stash apply # 应用但不删除stash
git stash list # 查看stash列表
git stash drop stash@{n} # 删除指定stash
git stash clear # 删除所有stash
8. git config
git config --global user.name "name" # 设置用户名
git config --global user.email "email" # 设置邮箱
git config --list # 查看所有配置
git config --global core.editor "vim" # 设置编辑器
git config --global alias.co checkout # 添加别名
9. git clean
git clean -n # 预览会删除什么
git clean -f # 删除未跟踪文件
git clean -fd # 删除未跟踪文件和目录
git clean -fx # 包括.gitignore中的内容也删
10. git blame / reflog / bisect / submodule
git blame <file> # 追踪每一行的提交
git reflog # 查看引用日志(包括被删除的提交)
git bisect start # 二分查找 bug
git submodule add <repo> <dir> # 添加子模块
git submodule update --init # 初始化/更新子模块
11. git archive / svn / gc / fsck
git archive -o out.zip HEAD # 导出zip包
git svn clone <url> # 从svn克隆
git gc # 回收/压缩仓库
git fsck # 检查仓库完整性
12. help & 其他
git help <command> # 查看帮助
git <command> --help # 查看帮助
0
Subscribe to my newsletter
Read articles from cattac directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
