Hacktoberfest 的 Git 速查表

2025-06-08

Hacktoberfest 的 Git 速查表

Hacktoberfest 即将到来。这里有一份 git 速查表,方便你以后进行 hacking:

0. Git 配置

主要文章:迁移到新机器时需要设置的七个 Git 配置

第一次使用 git 时(在新机器上),您应该设置一些配置。

# set name and email
git config --global user.name "Mohammad-Ali A'râbi"
git config --global user.email "my-name-at-work@employer.com"

# set default branch name
git config --global init.defaultBranch master

# set rebase the default pull policy
git config --global pull.rebase true

# set to auto-stash upon rebase(/pull)
git config --global rebase.autoStash true

# set to default branch name upon push
git config --global push.default current
Enter fullscreen mode Exit fullscreen mode

对于默认分支名称,main越来越受欢迎,正在取代旧的master。备选方案是trunkdevelop,所以请随意使用。

我们还将稍微讨论一下储藏。

1. 基本 Git 工作流程

这些命令是使用 Git 的基础。它们帮助你与仓库交互并管理你的工作目录。

克隆存储库

将远程存储库克隆到本地机器。

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

您可以从 repo 的 GitHub/GitLab 页面复制存储库 URL。

检查存储库状态

检查工作目录的状态。

git status
Enter fullscreen mode Exit fullscreen mode

此命令显示哪些文件发生了更改、哪些文件被添加以供提交等。

添加更改

提交之前暂存更改(将文件添加到暂存区)。

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

我将跳过“暂存所有更改”的命令,因为这是一个不好的做法。

提交更改

主要文章:Git 提交消息十诫

暂存后,提交更改并附带有意义的消息。

git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

查看提交历史记录

检查你的提交历史。

git log
Enter fullscreen mode Exit fullscreen mode

对于更简单的单行格式:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

将更改推送到远程

将已提交的更改推送到远程存储库。

git push
Enter fullscreen mode Exit fullscreen mode

2. 分支与合并

主要文章:在 Git 上使用分支的 7 个技巧

分支功能允许您独立处理项目的不同部分。以下是管理分支的方法:

创建新分支

为您的功能或错误修复创建一个新的分支。

git switch -c <branch-name>
Enter fullscreen mode Exit fullscreen mode

如果您只想创建新分支而不想切换到它:

git branch -c <branch-name>
Enter fullscreen mode Exit fullscreen mode

切换到另一个分支

主要文章:代替 Checkout 使用的 Git 命令

git switch <branch-name>
Enter fullscreen mode Exit fullscreen mode

列出所有分支

查看存储库中的所有分支。

git branch
Enter fullscreen mode Exit fullscreen mode

合并分支

将一个分支的更改合并到当前分支。

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

删除分支

一旦不再需要分支,您可以删除它。

# Locally
git branch -d <branch-name>

# Remotely
git push origin --delete <branch-name>
Enter fullscreen mode Exit fullscreen mode

3. 存储更改

存储临时存储您的更改而不提交它们。

将更改保存到储藏

如果您正在处理某件事并想在不提交的情况下切换分支:

git stash
Enter fullscreen mode Exit fullscreen mode

应用储藏

稍后检索您的更改:

git stash apply
Enter fullscreen mode Exit fullscreen mode

列出隐藏的更改

如果您有多个存储,您可以通过以下方式查看它们:

git stash list
Enter fullscreen mode Exit fullscreen mode

最后的话

以下文章也应该有用:

鏂囩珷鏉ユ簮锛�https://dev.to/aerabi/git-cheat-sheet-for-hacktoberfest-3a8h
PREV
仅使用 CSS 的多行打字机效果
NEXT
TypeScript 中的函数式编程