How to commit no change and new message?

GitCommit

Git Problem Overview


How can I make a new commit and create a new message if no changes are made to files?

Is this not possible since the commit's code (SHA ?) will be the same?

Git Solutions


Solution 1 - Git

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 http://www.kernel.org/pub/software/scm/git/docs/git-commit.html">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.

Solution 2 - Git

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

Solution 3 - Git

If I understood you right, you want to make an empty commit. In that case you need:

git commit --allow-empty

Solution 4 - Git

Maybe as a more sensible alternative, you could create an annotated tag (a named commit with a message). See the git tag -a option.

Solution 5 - Git

If you are using a system like gitversion It 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond-_-bView Question on Stackoverflow
Solution 1 - GitJeff BowmanView Answer on Stackoverflow
Solution 2 - GitBlazej SLEBODAView Answer on Stackoverflow
Solution 3 - GitDimaView Answer on Stackoverflow
Solution 4 - GitkanView Answer on Stackoverflow
Solution 5 - GitKevin JohnsonView Answer on Stackoverflow