Git 备忘单📄(50 条命令 + PDF 和海报)

2025-05-24

Git 备忘单📄(50 条命令 + PDF 和海报)

我厌倦了查找相同的常见 Git 命令 - 所以我制作了一个可以打印并贴在办公室墙上的备忘单。

此备忘单包含以下主题的 50 个常用 Git 命令:

  • 设置 Git
  • 启动项目
  • 做出改变
  • 基本概念
  • 分枝
  • 合并
  • 变基
  • 撤销操作
  • 审查你的仓库
  • 储藏
  • 同步本地和远程存储库

Git 命令速查表 PDF

一页 PDF 可轻松复制和粘贴命令。

点击此处下载 Git 命令速查表 PDF

PDF 和海报均有浅色模式和深色模式可供选择:

轻量模式下的 Git 速查表海报

黑暗模式下的 Git 速查表海报

Git 备忘单海报

订购一张实体 A3 海报贴在办公室墙上 - 这样您就可以快速查找命令,并将它们放在脑海顶部。

它采用厚实耐用的纸张,并具有哑光吸光表面。

点击此处订购 Git 速查表海报

这是我办公室墙上的:

我办公室墙上的 Git 备忘单海报

以下是备忘单中的所有命令:

设置

设置将附加到您的提交和标签的名称和电子邮件



$ git config --global user.name "Danny Adams"

$ git config --global user.email "myemail@gmail.com"


Enter fullscreen mode Exit fullscreen mode

使用 Git 启动项目

创建本地仓库(省略<directory>将当前目录初始化为 git 仓库)



$ git init <directory>


Enter fullscreen mode Exit fullscreen mode

下载远程仓库



$ git clone <url>


Enter fullscreen mode Exit fullscreen mode

做出改变

将文件添加到暂存区



$ git add <file>


Enter fullscreen mode Exit fullscreen mode

暂存所有文件



$ git add .


Enter fullscreen mode Exit fullscreen mode

将所有暂存文件提交到 git



$ git commit -m "commit message"


Enter fullscreen mode Exit fullscreen mode

添加对跟踪文件所做的所有更改并提交



$ git commit -am "commit message"


Enter fullscreen mode Exit fullscreen mode

Git 基本概念

main:默认开发分支
origin:默认上游仓库
HEAD:当前分支
HEAD^:HEAD 的父级
HEAD~4:HEAD 的曾曾祖父母

分支

列出所有本地分支。添加-r标志以显示所有远程分支。-a所有分支的标志。



$ git branch


Enter fullscreen mode Exit fullscreen mode

创建新分支



$ git branch <new-branch>


Enter fullscreen mode Exit fullscreen mode

切换到分支并更新工作目录



$ git checkout <branch>


Enter fullscreen mode Exit fullscreen mode

创建新分支并切换到该分支



$ git checkout -b <newbranch>


Enter fullscreen mode Exit fullscreen mode

删除合并的分支



$ git branch -d <branch>


Enter fullscreen mode Exit fullscreen mode

删除分支,无论是否
合并



$ git branch -D <branch>


Enter fullscreen mode Exit fullscreen mode

为当前提交添加标签(通常用于新版本发布)



$ git tag <tag-name>


Enter fullscreen mode Exit fullscreen mode

合并

将分支合并a到分支b。添加--no-ff非快速合并选项

Git 中的快进与非快进合并



$ git checkout b

$ git merge a


Enter fullscreen mode Exit fullscreen mode

合并并压缩所有提交为一个新的提交



$ git merge --squash a


Enter fullscreen mode Exit fullscreen mode

变基

将功能分支变基到主分支(以合并主分支的新更改)。防止不必要的合并提交到功能分支,保持历史记录清晰

在 Git 中将功能重新定位到主功能上



$ git checkout feature

$ git rebase main


Enter fullscreen mode Exit fullscreen mode

在重新定位到主分支之前,以交互方式清理分支提交



$ git rebase -i main


Enter fullscreen mode Exit fullscreen mode

以交互方式重新定位当前分支上的最后 3 个提交



$ git rebase -i Head~3


Enter fullscreen mode Exit fullscreen mode

撤销

移动(和/或重命名)文件并移动阶段



$ git mv <existing_path> <new_path>


Enter fullscreen mode Exit fullscreen mode

从工作目录和暂存区中删除文件,然后暂存删除操作



$ git rm <file>


Enter fullscreen mode Exit fullscreen mode

仅从暂存区移除



$ git rm --cached <file>


Enter fullscreen mode Exit fullscreen mode

查看先前的提交(只读)



$ git checkout <commit_ID>


Enter fullscreen mode Exit fullscreen mode

创建新的提交,恢复指定提交的更改



$ git revert <commit_ID>


Enter fullscreen mode Exit fullscreen mode

回到上一个提交并删除之前的所有提交(还原更安全)。添加--hard标志以同时删除工作区更改(请务必小心)



$ git reset <commit_ID>


Enter fullscreen mode Exit fullscreen mode

审查你的 Repo

列出尚未提交的新的或修改的文件



$ git status


Enter fullscreen mode Exit fullscreen mode

列出提交历史记录,并附带相应的 ID



$ git log --oneline


Enter fullscreen mode Exit fullscreen mode

显示未暂存文件的更改。对于暂存文件的更改,添加--cached选项



$ git diff


Enter fullscreen mode Exit fullscreen mode

显示两次提交之间的更改



$ git diff commit1_ID commit2_ID


Enter fullscreen mode Exit fullscreen mode

储藏

存储已修改和暂存的更改。要包含未跟踪的文件,请添加-u标记。对于未跟踪和已忽略的文件,也请添加-a标记。



$ git stash


Enter fullscreen mode Exit fullscreen mode

同上,但添加一条注释。



$ git stash save "comment"


Enter fullscreen mode Exit fullscreen mode

部分存储。仅存储单个文件、文件集合或文件内部的个别更改



$ git stash -p


Enter fullscreen mode Exit fullscreen mode

列出所有储藏



$ git stash list


Enter fullscreen mode Exit fullscreen mode

重新应用存储但不删除它



$ git stash apply


Enter fullscreen mode Exit fullscreen mode

重新应用索引 2 处的存储,然后将其从存储列表中删除。省略此操作stash@{n}将弹出最新的存储。



$ git stash pop stash@{2}


Enter fullscreen mode Exit fullscreen mode

显示存储 1 的差异摘要。传递-p标志以查看完整差异。



$ git stash show stash@{1}


Enter fullscreen mode Exit fullscreen mode

删除索引 1 处的存储。省略stash@{n}以删除最后一个存储



$ git stash drop stash@{1}


Enter fullscreen mode Exit fullscreen mode

删除所有存储



$ git stash clear


Enter fullscreen mode Exit fullscreen mode

同步

添加远程仓库



$ git remote add <alias> <url>


Enter fullscreen mode Exit fullscreen mode

查看所有远程连接。添加-v标志以查看 URL。



$ git remote


Enter fullscreen mode Exit fullscreen mode

删除连接



$ git remote remove <alias>


Enter fullscreen mode Exit fullscreen mode

重命名连接



$ git remote rename <old> <new>


Enter fullscreen mode Exit fullscreen mode

从远程仓库获取所有分支(不合并)



$ git fetch <alias>


Enter fullscreen mode Exit fullscreen mode

获取特定分支



$ git fetch <alias> <branch>


Enter fullscreen mode Exit fullscreen mode

获取远程仓库当前分支的副本,然后合并



$ git pull


Enter fullscreen mode Exit fullscreen mode

将本地更改移动(重新定基)到对远程仓库所做的新更改的顶部(以获得干净、线性的历史记录)



$ git pull --rebase <alias>


Enter fullscreen mode Exit fullscreen mode

将本地内容上传到远程仓库



$ git push <alias>


Enter fullscreen mode Exit fullscreen mode

上传到分支(然后可以拉取请求)



$ git push <alias> <branch>

Enter fullscreen mode Exit fullscreen mode




感谢阅读

希望这份备忘单有用!

再次,请随意下载单页 PDF 或订购海报:
单页 Git 命令备忘单 PDF
订购实体海报

欲了解我的更多信息,您可以在 Twitter 上关注我

干杯!

文章来源:https://dev.to/doabledanny/git-cheat-sheet-50-commands-free-pdf-and-poster-4gcn
PREV
Vue 生态系统开发者指南什么是 Vue 社区指南?
NEXT
11 个 Web 开发者的 UI 设计技巧