另一个 Git 备忘单

2025-05-27

另一个 Git 备忘单

图片来源https://acodez.in/gitlab-vs-github-vs-bitbucket/

Git 是什么

Git 是当今世界广泛使用的免费开源分布式版本控制系统。它有助于在软件开发过程中跟踪源代码的更改。它旨在协调程序员之间的工作,但也可用于跟踪任何文件集的更改。其目标包括速度、数据完整性以及对分布式非线性工作流的支持。

什么是版本控制

版本控制会追踪代码的每一次修改。如果出现错误,开发人员可以比较早期版本的代码来帮助修复错误,同时最大限度地减少对所有团队成员的干扰。

如何使用此备忘单

如果您需要参考任何 Git 命令,或者不确定要使用的语法,请随时查看此备忘单并按“Cmd + f”(Mac 用户)或“Ctl + f”(Windows 用户)来查找任何相关详细信息。

其他有用资源

从头开始创建新的 Git Repo
高级 Git 备忘单
Git
OhShitGit
Git:备忘单(高级)

* 注意:*

每当我想要记录新的命令时,这个备忘单就会进行进一步更新,以便我将来可以再次查看。


1. Git 基础

在指定目录中创建空的 Git 仓库。不带参数运行,将当前目录初始化为 git 仓库。

git init <directory>
Enter fullscreen mode Exit fullscreen mode

将位于 的仓库克隆到<repo>本地机器上。原始仓库可以位于本地文件系统,也可以通过 HTTP 或 SSH 位于远程机器上。

git clone <repo>
Enter fullscreen mode Exit fullscreen mode

将所有更改暂存<directory>到下次提交。替换<directory>
<file>可更改特定文件。

git add <directory>   # to add specific dir
git add .             # to add all that are not staged for the next commit
Enter fullscreen mode Exit fullscreen mode

提交未暂存的快照是一个好习惯。提交暂存的快照,但不要启动文本编辑器,<message>而是使用提交消息。

git commit -m "<message>"
Enter fullscreen mode Exit fullscreen mode

将最后一次提交替换为已暂存的更改和最后一次提交的组合。使用 with nothing staged 可编辑最后一次提交的消息。

git commit --amend
Enter fullscreen mode Exit fullscreen mode

列出哪些文件已暂存、哪些文件未暂存、哪些文件未跟踪。

git status
Enter fullscreen mode Exit fullscreen mode

2. 日志记录

使用默认格式显示完整的提交历史记录。如需自定义,请参阅其他选项。

git log
Enter fullscreen mode Exit fullscreen mode

限制提交次数<limit>。例如,git log -5 将限制为 5 次提交。

git log -<limit>
Enter fullscreen mode Exit fullscreen mode

将每次提交压缩为一行。

git log --oneline
Enter fullscreen mode Exit fullscreen mode

显示每次提交的完整差异。

git log -p
Enter fullscreen mode Exit fullscreen mode

包括哪些文件被修改以及每个文件中添加或删除的行数的相对值。

git log -stat
Enter fullscreen mode Exit fullscreen mode

搜索特定作者的提交

git log --author="<pattern>"
Enter fullscreen mode Exit fullscreen mode

搜索具有匹配的提交消息的提交<pattern>

git log --grep="<pattern>"
Enter fullscreen mode Exit fullscreen mode

<since>显示在和之间发生的提交<until>。参数可以是提交 ID、分支名称、HEAD 或任何其他类型的修订引用。

git log <since>..<until>
Enter fullscreen mode Exit fullscreen mode

仅显示具有指定文件的提交。

git log -- <file>
Enter fullscreen mode Exit fullscreen mode

--graph标志在提交消息的左侧绘制基于文本的提交图表。--decorate添加显示的提交的分支名称或标签。

git log --graph --decorate
Enter fullscreen mode Exit fullscreen mode

3. 检查差异

显示索引和工作目录之间的未暂存更改。

git diff
Enter fullscreen mode Exit fullscreen mode

显示工作目录和上次提交之间的差异。

git diff HEAD
Enter fullscreen mode Exit fullscreen mode

显示暂存更改和上次提交之间的差异。

git diff --cached
Enter fullscreen mode Exit fullscreen mode

4. 撤消更改或重写历史记录

创建新的提交以撤消所做的所有更改<commit>,然后将其应用到当前分支。

git revert <commit>
Enter fullscreen mode Exit fullscreen mode

重置暂存区以匹配最近的提交,但保持工作目录不变。

git reset
Enter fullscreen mode Exit fullscreen mode

<file>从暂存区移除文件,但保留工作目录不变。这将取消暂存文件,但不覆盖任何更改。

git reset <file>
Enter fullscreen mode Exit fullscreen mode

重置暂存区和工作目录以匹配最近的提交并覆盖工作目录中的所有更改

git reset --hard
Enter fullscreen mode Exit fullscreen mode

将当前分支尖端向后移动到<commit>,重置暂存区以匹配,但保留工作目录

git reset <commit>
Enter fullscreen mode Exit fullscreen mode

重置暂存区和工作目录以使其匹配。删除未提交的更改以及之后的所有提交<commit>

git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode

显示哪些文件将被从工作目录中删除。使用 -f 标志代替 -n 标志来执行清理操作。

git clean -n
Enter fullscreen mode Exit fullscreen mode

将当前分支重新定位到<base><base>可以是提交 ID、分支名称、标签或 HEAD 的相对引用。

Git merge 始终是向前移动的更改记录。或者,rebase 具有强大的历史记录重写功能。

git rebase <base>
Enter fullscreen mode Exit fullscreen mode

以交互方式将当前分支 rebase 到 。启动编辑器以输入命令,指示如何将每个提交转移到新的基

git rebase -i <base>
Enter fullscreen mode Exit fullscreen mode

显示本地仓库 HEAD 的更改日志。添加--relative-date标志以显示日期信息或--all显示所有引用。

git reflog
Enter fullscreen mode Exit fullscreen mode

4. Git 分支

列出你的仓库中的所有分支。添加一个<branch>参数来创建一个新的分支,名称为<branch>

git branch
Enter fullscreen mode Exit fullscreen mode

创建并检出一个名为 的新分支<branch>。删除 -b 标志可检出现有分支。

got checkout -b <branch>
Enter fullscreen mode Exit fullscreen mode

合并<branch>到当前分支。

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

5. 远程存储库

创建与远程仓库的新连接。添加远程仓库后,您可以将其用作其他命令<name>的快捷方式。<url>

git remote add <name> <url>
Enter fullscreen mode Exit fullscreen mode

从仓库中获取特定的<branch>。暂停<branch>以获取所有远程引用。

git fetch <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

获取指定远程的当前分支副本并立即将其合并到本地副本中。

git pull <remote>
Enter fullscreen mode Exit fullscreen mode

获取当前分支的远程副本,并将其变基为本地副本。使用 git rebase 而不是 merge 来整合分支。

rebase 命令可以将一个分支的更改合并到另一个分支。它是更广为人知的“merge”命令的替代命令。
最明显的一点是,rebase 与 merge 的区别在于,它会重写提交历史记录,从而生成直线、线性的连续提交。

git pull --rebase <remote>
Enter fullscreen mode Exit fullscreen mode

将分支<remote>以及必要的提交和对象推送到 。如果远程仓库中不存在指定分支,则创建该分支。

git push <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

强制执行 git push,即使它会导致非快进合并。--force除非你非常确定自己在做什么,否则请不要使用此标志。

git push <remote> --force
Enter fullscreen mode Exit fullscreen mode

将所有本地分支推送到指定的远程

git push <remote> --all
Enter fullscreen mode Exit fullscreen mode

推送分支或使用 flag 时,标签不会自动推送--all。该--tagsflag 会将所有本地标签发送到远程仓库。

git push <remote> --tags
Enter fullscreen mode Exit fullscreen mode

6. Git 配置

定义当前仓库中所有提交的作者名称。开发者通常使用--global标志来设置当前用户的配置选项。

git config user.name <name>
Enter fullscreen mode Exit fullscreen mode

定义当前用户所有提交的作者姓名

git config --global user.name <name>
Enter fullscreen mode Exit fullscreen mode

定义当前用户所有提交所使用的作者电子邮件。

git config --global user.email <email>
Enter fullscreen mode Exit fullscreen mode

为 Git 命令创建快捷方式。

git config --global alias. <alias-name> <git-command>
Enter fullscreen mode Exit fullscreen mode

设置机器上所有用户的命令使用的文本编辑器。arg<editor>应该是启动所需编辑器的命令(例如 vi)。

git config --system core.editor <editor>
Enter fullscreen mode Exit fullscreen mode

在文本编辑器中打开全局配置文件进行手动编辑。

git confg --global --edit
Enter fullscreen mode Exit fullscreen mode

7. 临时存储已修改、已跟踪的文件以便更改分支

保存已修改和已阶段性的变更。

git stash
Enter fullscreen mode Exit fullscreen mode

列出存储文件更改的堆栈顺序。

git stash list
Enter fullscreen mode Exit fullscreen mode

从存储堆栈顶部写入工作。

git stash pop
Enter fullscreen mode Exit fullscreen mode

丢弃存储堆栈顶部的更改

git stash drop
Enter fullscreen mode Exit fullscreen mode

8.其他有用的 Git 命令

显示 Git 远程 URL,或者如果您未连接到可以到达远程存储库的网络。

git config --get remote.origin.url
Enter fullscreen mode Exit fullscreen mode

如果您所在的网络可以访问源所在的远程存储库,则显示 Git 远程 URL。

git remote show origin
Enter fullscreen mode Exit fullscreen mode

Git 将现有 repo 推送到新的不同的远程 repo 服务器。

# Create a new repo at github.
# Copy the new Git repo URL.
git remote rename origin upstream
git remote add origin <NEW_GIT_REPO_URL>
git push origin master
Enter fullscreen mode Exit fullscreen mode
文章来源:https://dev.to/henryong92/yet-another-git-cheatsheet-4gjk
PREV
如何使用 Google Drive 在 3 分钟内免费部署静态网站
NEXT
从零到 15 — 15 分钟内构建 Nothing PWA