git 如何提交不更改和新消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12470029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to commit no change and new message?
提问by d-_-b
How can I make a new commit
and create a new message if no changes are made to files?
commit
如果没有对文件进行任何更改,如何创建新消息并创建新消息?
Is this not possible since the commit's code (SHA ?) will be the same?
这是不可能的,因为提交的代码(SHA?)将是相同的?
回答by Jeff Bowman
There's rarely a good reason to do this, but the parameter is --allow-empty
for empty commits, in contrast to --allow-empty-message
for empty messages. You can also read more by typing git help commit
or visiting the online documentation.
很少有充分的理由这样做,但该参数--allow-empty
用于空提交,与--allow-empty-message
空消息相反。您还可以通过键入git help commit
或访问在线文档来阅读更多信息。
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.
虽然树对象(它有自己的散列)是相同的,但提交实际上会有不同的散列,因为它可能有不同的时间戳和消息,并且肯定会有不同的父提交。所有这三个因素都集成到git
的对象哈希算法中。
There area 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.
- 作为“声明性提交”,添加叙述或文档(通过DavidNeiss),包括关于通过测试或 lint 的事后数据(通过Robert Balicki)。
- 在
git
不生成任意更改的情况下测试命令(通过Vaelus)。 - 使用
gitolite
(通过Tatsh)重新创建已删除的裸存储库。 - 任意创建新提交,例如重新触发构建工具(通过mattLummus)或为了个人日志记录或指标(通过DynamiteReed)。但是,请三思:根据您的分支/合并结构,提交可能会持续很长时间,因此“什么都不提交”策略可能会无意中用临时工作流工件污染您团队的存储库,并使代码修订与短暂的残骸。
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 Tagsfor 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.
回答by Dima
If I understood you right, you want to make an empty commit. In that case you need:
如果我理解你的正确,你想要做一个空的提交。在这种情况下,您需要:
git commit --allow-empty
回答by Adobels
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
回答by Kevin Johnson
If you are using a system like gitversionIt makes a lot of sense to do this sort of commit. You could have a commit that is specifically for bumping the major version using a +semver: major comment.
如果你使用的是像gitversion这样的系统,那么做这种提交是很有意义的。你可以有一个专门用于使用 +semver:major 注释来改变主要版本的提交。
回答by kan
Maybe as a more sensible alternative, you could create an annotated tag(a named commit with a message). See the git tag -a
option.
也许作为更明智的选择,您可以创建一个带注释的标签(带有消息的命名提交)。查看git tag -a
选项。