创建空提交

GitGitBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在 Git 中,提交(commit)是对存储库所做更改的快照。每个提交都有一条描述所做更改的消息。有时,你可能需要创建一个没有更改的空提交,用作占位符或触发构建过程。在本实验中,你将学习如何在 Git 中创建空提交。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/commit("`Create Commit`") subgraph Lab Skills git/commit -.-> lab-12716{{"`创建空提交`"}} end

创建空提交

你需要在你的 Git 存储库中创建一个空提交。这在几种情况下可能会很有用,例如:

  • 触发构建过程
  • 创建一个占位符提交
  • 在存储库的历史记录中标记一个特定点

对于本实验,让我们使用来自 https://github.com/labex-labs/git-playground 的存储库:

  1. 使用命令 git clone https://github.com/labex-labs/git-playground 将存储库克隆到你的本地机器。
  2. 使用命令 cd git-playground 导航到存储库的目录,并使用命令 git config --global user.name "你的用户名"git config --global user.email "你的邮箱" 在环境中配置你的 GitHub 账户。
  3. 使用命令 git commit --allow-empty -m "Empty commit" 创建一个消息为 "Empty commit" 的空提交。
  4. 使用命令 git log --name-status HEAD^..HEAD 验证空提交是否已创建。

这是你运行 git log --name-status HEAD^..HEAD 后的结果:

git log 空提交结果

总结

在 Git 中创建空提交是一个简单的过程,在几种情况下可能会很有用。使用 git commit --allow-empty -m <消息> 命令来创建带有指定消息的空提交。

您可能感兴趣的其他 Git 教程