Git 备忘速查手册¶
Creating Projects¶
git init¶
git clone¶
Basic Commands¶
git status¶
查看当前存储库中文件的状态,以帮助您了解工作目录、暂存区和存储库之间的差异。
git add¶
git diff¶
git diff-显示提交、提交和工作树等之间的变化。
# 显示所有未添加至index的变更
git diff
# 和commit记录的差异
git diff f2a9c2cad88cfe2206dc7e6c271d673365723add
# 显示所有已添加index但还未commit的变更
git diff --cached
# 比较与上一个版本的差异
git diff HEAD^
git commit¶
# 提交全部已追踪变更
git commit -m 'add file.'
# 添加且提交一个已追踪的文件变更
# 对新建文件无效
git commit -am 'update a tracked file.'
# 修改上次提交消息内容
git commit --amend
# 上次提交新增变更内容, 不修改提交消息内容
git commit --amend --no-edit
git reset¶
重置当前分支的HEAD到指定的状态。这个命令可以用来撤销提交、恢复工作目录中的文件等。git reset 的行为取决于你选择的模式:--soft, --mixed, --hard, --merge, 或 --keep。下面我们来详细介绍这些选项及其用途。
Command
Option
--soft
: 仅移动 HEAD 到指定的提交,不会改变暂存区或工作目录。--mixed
: 默认,移动 HEAD 并重置暂存区以匹配目标提交,但不改变工作目录。这意味着你的更改仍然保留在工作目录中,但不再是已暂存状态。--hard
: 移动 HEAD,并且重置暂存区和工作目录至目标提交。所有未提交的更改都将丢失。--merge
:如果你的当前工作目录中有未合并的更改,使用 --merge 可以尝试合并这些更改到目标提交。如果存在冲突,则重置过程会被中断。--keep
:试图保持工作目录中的更改不变,只移动 HEAD 并更新索引(暂存区)。
# 撤消最近的提交并保留更改
git reset HEAD~1
# 撤消N个最近的提交并保留更改
git reset HEAD~N
# 撤消最近的提交并删除更改
git reset --hard HEAD~1
git rm¶
命令 | 删除工作区 | 通知 Git | 自动暂存 | 需要Add | 风险 |
---|---|---|---|---|---|
rm file | ✅ | ❌ | ❌ | ✅ 需要 | 低 |
git rm file | ✅ | ✅ | ✅ | ❌ 不需要 | 中 |
git rm -r folder | ✅ | ✅ | ✅ | ❌ 不需要 | ⚠️ 高 |
git rm file.md
git rm -r folder
# On branch main
# Changes to be committed:
# (use "git restore --staged <file>..." to unstage)
# deleted: start.md
# deleted: sub_folder/sub_file.md
git gc¶
全称是 git garbage collect,清理仓库中不必要的文件,压缩数据,优化仓库性能。
- 合并小的 pack 文件
- 删除不可达对象(垃圾)
- 压缩仓库体积
- 提升 Git 操作速度
git fsck¶
检查仓库完整性
- 检测损坏的对象
- 查找孤立对象
- 验证连接性
- 报告潜在问题
Option
--full
默认情况下,git fsck 可能会跳过某些优化检查;使用此选项可进行全面检查。--strict
对检查过程更加严格,可能会报告更多潜在但不一定致命的问题。--lost-found
将孤立的或者有问题的对象复制到 .git/lost-found 目录下,以便进一步分析或恢复。
Branching and Merging¶
git branch¶
git branch
git branch --list
git branch -r
git branch -a
# 创建分支 git branch [branchName]
git branch dev
# 创建并切换分支
git checkout -b new-branch
# 当前分支合并情况 git branch [--merged] [--no-merged]
# 提交已合并到当前分支的分支
git branch --merged
# 提交未全部合并到当前分支的分支
git branch --no-merged
# 本地分支名称 git branch [-m|-M] <oldName> <newName>
git branch -m test test_new
git checkout¶
# git checkout [-q] [-f|-m] dev
# -q 安静模式(quiet)
# -f 强制(force)丢弃修改;
# -m 合智能合并(migrate,或 --merge)
git checkout dev
# git checkout tag
git checkout v2.0
# git checkout remote branch and create local branch
# git checkout remote branch auto create local branch
git checkout --track origin/dev
git checkout -b dev origin/dev
# 创建并切换分支
git checkout -b new-branch
# 删除本地分支
git branch -d test_new
git merge¶
git merge-将两个或多个开发历史连接在一起,目标分支合并到当前分支。
# 基本合并
# 要合并进当前分支的目标分支名git merge <branch>
git checkout main
git merge dev
# 禁用快进合并
git merge --no-ff feature
# 压缩合并
# 将 feature 分支的所有更改压缩成一个提交:
git merge --squash feature
git commit -m "Squashed and merged feature branch"
git rebase¶
git rebase 是 Git 中一个强大的命令,用于将一个分支的更改重新应用到另一个分支的顶部。相比于 git merge,它提供了一种线性的方式来整合更改,使得提交历史更加清晰和平整。
# git rebase [<upstream>]
git rebase main
# 远程仓库的基础上工作,并希望拉取最新更改而不创建合并提交
# git pull --rebase 等同于先 git fetch 然后 git rebase origin/main。
git pull --rebase origin main
# 变基过程中对提交进行修改(如压缩、编辑或重排),可以使用交互式变基
# n 是你想回溯的提交数量。Git 会打开一个文本编辑器,允许你指定对每个提交的操作。
git rebase -i HEAD~n
git log¶
Command
Example
git log
git log -n
# 更多信息: 变更行数
git log --stat
# 更多信息: 变更内容 + 提交记录
git log -p -m
# git log <tag name>
git log v0.0.1
git stash¶
# 暂存当前修改
git stash
# 查看所有暂存列表
git stash list
# 查看最暂存详情
git stash show -p stash@{0}
# 应用暂存
git stash pop [--index] [-q | --quiet] [<stash>]
git stash apply stash@{0}
# 暂存转新分支 git stash branch <branchName> [<stash>]
git stash branch dev_new stash@{0}
# 删除暂存
# git stash drop [-q | --quiet] [<stash>]
git stash drop stash@{0}
git tag¶
git tag
git tag -a v0.0.1 -m 'add a tag.'
# 删除 git tag -d <tagName>
git tag -d v0.0.2
# git tag -v <tagName>
git tag -v v0.0.1
# git show <tag name>
git show v0.0.1
# git log <tag name>
git log v0.0.1
Sharing and Updating Projects¶
git push¶
git remote¶
# 列表
git remote
# 丰富信息列表
git remote -v
git remote -v show <origin>
# 添加远程仓库名称和地址
git remote add origin git@github.com:xxx/git-demo.git
# 重命名远程仓库名称 git remote rename --no-progress <old> <new>
git remote rename origin origin_new
# 移除远程仓库:git remove <name>
git remove origin
# 获取远程仓库地址:git remote get-url [--push] [--all] <name>
git remote get-url origin
# 修改远程仓库地址
git remote set-url origin <new_url> <old_url>
git remote set-url origin --add https://github.com/xxx/git-demo.git
git remote set-url --delete origin https://github.com/xxx/git-demo.git
# Fetching: git remote [-v] update <name>
git remote [-v] update origin
# 删除与<name>关联的过时引用。默认情况下,删除<name>下的过时远程跟踪分支,但根据全局配置和远程配置,我们甚至可能会修剪尚未推送到那里的本地标签
# git remote prune [-n | --dry-run] <name>…
Inspection and Comparison¶
git show¶
# git log
# git show hash
git show d4d30167d01d10d7de5e7aca699863dc27735678
git show d4d30
# git show <tag name>
git show v0.0.1
# git show HEAD
# 当前提交
git show HEAD
git show HEAD~0
# 第一父链, 父提交
git show HEAD^ # git show HEAD~1
git show HEAD^~0
git show HEAD^1
git show HEAD^1~0
# 第一父链, 父提交的父提交,第二父提交
git show HEAD^^
git show HEAD^~1
# 第二父链:HEAD 是一个合并提交, 有2个以上父提交
git show HEAD^2
git show HEAD^2~0
git show HEAD@{2}
git show main@{yesterday}
Note
^
(脱字符):表示“父提交”HEAD^
=HEAD^1
:表示 HEAD 的第一个父提交,当前父链,第一父链。HEAD
=HEAD~0
:当前提交HEAD^
=HEAD^1~0
=HEAD~1
:表示 HEAD 的第一个父提交(也就是上一个提交)。HEAD^^
=HEAD^1~1
=HEAD~2
:表示 HEAD 的父提交的父提交,即“祖父提交”.HEAD^^^
=HEAD^1~2
=HEAD~3
: 当前父链第三提交.
HEAD^2
为当前HEAD的第二父提交,HEAD 是一个合并提交, 有2个以上父提交。
Setup And Config¶
Global Config¶
Set the global config.