初始化新的本地仓库
$ git init [project name]
克隆远程仓库
$ git clone git_url
克隆远程仓库到指定目录
$ git clone git_url my_directory
查看工作区与暂存区的文件状态
$ git status
将指定文件添加到暂存区
$ git add [file]
将所有变更文件添加到暂存区
$ git add .
将暂存区内容提交到版本历史
$ git commit -m "commit message"
提交所有已被追踪的变更文件
$ git commit -am "commit message"
丢弃工作区未暂存的修改
$ git restore [file]
取消暂存指定文件
$ git restore --staged [file]
重置暂存区文件,保留工作区修改
$ git reset [file]
强制重置工作区和暂存区到上一次提交
$ git reset --hard
对比工作区与暂存区的差异
$ git diff
对比暂存区与最新提交的差异
$ git diff --staged
将当前分支变基到指定分支
$ git rebase [branch]
配置提交记录中显示的用户名
$ git config --global user.name "name"
配置提交记录中显示的邮箱地址
$ git config --global user.email "email"
开启 Git 终端彩色输出
$ git config --global color.ui auto
使用默认编辑器打开全局配置文件
$ git config --global --edit
列出所有本地分支
$ git branch
列出所有本地分支与远程分支
$ git branch -av
切换到指定分支并更新工作区
$ git checkout my_branch
创建并立即切换到新分支
$ git checkout -b new_branch
删除指定本地分支
$ git branch -d my_branch
将 branchA 合并到 branchB
$ git checkout branchB
$ git merge branchA
为当前提交打标签
$ git tag my_tag
查看当前分支的提交历史记录
$ git log
查看 branchA 相比 branchB 独有的提交
$ git log branchB..branchA
查看指定文件的变更历史(含重命名)
$ git log --follow [file]
对比 branchA 和 branchB 之间的文件差异
$ git diff branchB...branchA
查看 Git 对象(提交、标签等)的详细信息
$ git show [SHA]
拉取远程仓库的所有分支更新
$ git fetch [alias]
将远程分支合并到当前分支
$ git merge [alias]/[branch]
# 非 Fast-forward 合并
$ git merge --no-ff [alias]/[branch]
# 仅允许 Fast-forward 合并
$ git merge --ff-only [alias]/[branch]
推送本地分支提交到远程仓库
$ git push [alias] [branch]
拉取远程更新并直接合并到当前分支
$ git pull
拣选指定提交合并到当前分支
$ git cherry-pick [commit_id]
添加远程仓库地址别名
$ git remote add [alias] [url]
显示已绑定的远程仓库别名
$ git remote
显示已绑定的远程仓库名称与详细 URL
$ git remote -v
移除已绑定的远程仓库
$ git remote rm [remote repo name]
修改远程仓库的 URL 地址
$ git remote set-url origin [git_url]
暂存当前未提交的修改与暂存区内容
$ git stash
列出所有已暂存的内容列表
$ git stash list
恢复最新一次暂存的内容并从栈中移除
$ git stash pop
丢弃最新一次暂存的内容
$ git stash drop
从项目中删除文件并暂存该操作
$ git rm [file]
移动或重命名文件并暂存该操作
$ git mv [existing-path] [new-path]
查看提交历史及文件移动/重命名统计
$ git log --stat -M
/logs/*
# "!" 表示不忽略指定文件
!logs/.gitkeep
# 忽略 Mac 系统文件
.DS_store
# 忽略 node_modules 依赖目录
node_modules
# 忽略 SASS 缓存文件
.sass-cache
.gitignore 文件用于配置 Git 故意忽略、不进行版本追踪的文件规则。
按文件内容变更检索提交
$ git log -S'<a term in the source>'
查看指定文件随时间变化的详细 diff 历史
$ git log -p <file_name>
以图形化单行格式输出漂亮的提交树
$ git log --pretty=oneline --graph --decorate --all
列出指定提交中修改过的文件
$ git log --name-only <commit-id>
列出提交中修改过的文件(排除 Merge 提交)
$ git log --no-merges --name-only
限制显示最新的 N 条提交记录
$ git log -<number>
限制显示最新的 N 条提交记录(替代语法)
$ git log -n <number>
限制最多显示 N 条提交
$ git log --max-count=<number>
筛选特定日期之后的提交
$ git log --after="YYYY-MM-DD HH:MM:SS ±HHMM"
筛选特定日期之前的提交
$ git log --before="YYYY-MM-DD HH:MM:SS ±HHMM"
按提交者姓名筛选提交记录
$ git log --author=<pattern>
按提交信息关键字筛选提交记录
$ git log --grep=<pattern>
显示当前分支带引用标记的提交树图形
$ git log --graph --decorate
在不切换分支的情况下查看指定分支的提交图形
$ git log --graph --decorate <branch>
查看 branchA 相比 branchB 的提交差异图形
$ git log --graph --decorate <branchB>..<branchA>
显示所有分支的提交关系图形
$ git log --graph --decorate --all
列出所有分支及其对应的上游跟踪分支
$ git branch -vv
快速切回上一个工作分支
$ git checkout -
只列出远程分支
$ git branch -r
从其他分支检出单个文件覆盖当前工作区
$ git checkout <branch> -- <file>
修改最新一次提交的 commit message
$ git commit --amend -m "new message"
补充提交到最新记录且不修改提交信息
$ git commit --amend --no-edit
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
在指定路径创建全新的工作树目录。若指定分支则检出该分支,否则以 HEAD 为基础创建同名新分支。
$ git worktree add <path> [<branch>]
列出当前仓库关联的所有活动工作树
$ git worktree list
移除指定路径的工作树
$ git worktree remove <path>
清理失效或已被手动删除的工作树记录
$ git worktree prune
将工作树移动到新的路径
$ git worktree move <old_path> <new_path>
在当前仓库中添加新子模块:
$ git submodule add <repository_url> <path>
递归克隆主仓库及其所有子模块:
$ git clone --递归处理 <repository_url>
将当前仓库中的所有子模块更新至各自分支的最新提交:
$ git submodule update
拉取子模块远程仓库的最新变更并更新主仓库引用:
$ git submodule update --remote
从当前仓库中移除指定子模块:
$ git submodule deinit <path>
$ git rm <path>
$ git commit -m "Removed submodule"
拣选提交允许你从另一个分支挑选特定的提交应用到当前分支:
$ git cherry-pick <commit_hash>
显示引用日志,记录 HEAD 指针与分支变动的完整轨迹:
$ git reflog
通过 reflog 找到误删的 commit 或分支哈希,并检出该哈希进行恢复:
$ git checkout <commit_or_branch_hash>