How to commit no change and new message?

How to commit no change and new message?

回答1

There's rarely a good reason to do this, but the parameter is --allow-empty for empty commits (no files changed), in contrast to --allow-empty-message for empty commit messages. You can also read more by typing git help commit or visiting the online documentation.

While the tree object (which has a hash of its own) will be identical, the commit will actually have a different hash, because it will presumably have a different timestamp and message, and will definitely have a different parent commit. All three of those factors are integrated into git's object hash algorithm.


There are a few reasons you might want an empty commit (incorporating some of the comments):

  • As a "declarative commit", to add narration or documentation (via DavidNeiss) including after-the-fact data about passing tests or lint (via Robert Balicki).
  • To test git commands without generating arbitrary changes (via Vaelus).
  • To re-create a deleted bare repository using gitolite (via Tatsh).
  • To arbitrarily create a new commit, such as for re-triggering build tooling (via mattLummus) or for the sake of personal logging or metrics (via DynamiteReed). However, think twice: depending on your branch/merge structure, commits may live for a very long time, so a "just commit nothing" strategy may be inadvertently pollute your team's repository with temporary workflow artifacts and make it hard to separate code revisions from ephemeral cruft.

Other strategies to add metadata to a commit tree include:

  • Separate branches or lightweight tags that always point to a commit of a particular status (e.g. "last accepted commit" or "current staging commit").
  • Annotated Tags for a way to record timestamp, committer, and message, pointing to an existing commit without adding an entry in the commit tree itself.
  • git notes to associate a mutable note on top of an existing immutable commit.

使用allow empty提交新的commit

 

回答2

Empty commit with a message

git commit --allow-empty -m "Empty test commit"

Empty commit with an empty message

git commit --allow-empty --allow-empty-message

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-01-15 System.Security.SecurityException when writing to Event Log
2021-01-15 4010 An unhandled security exception has occurred. IIS 8.5
2021-01-15 How to make a grid (like graph paper grid) with just css?
2019-01-15 margin和text-align以及align
2019-01-15 inline-block和同级的text-align问题
2019-01-15 How to choose from Viewstate, SessionState, Cookies and Cache
2019-01-15 C#中的转义字符verbatim string
点击右上角即可分享
微信分享提示