How to commit no change and new message?

103,935

Solution 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.

Solution 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

Solution 3

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

git commit --allow-empty

Solution 4

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

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.

Share:
103,935

Related videos on Youtube

d-_-b
Author by

d-_-b

(your about me is currently blank)

Updated on July 14, 2020

Comments

  • d-_-b
    d-_-b almost 4 years

    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?

  • Novice C
    Novice C almost 8 years
    I've started to follow the git flow branch model. When you make a dev branch form master and then a feat branch immediately from dev, the feat branch looks to come from the master branch as there is no distinguishing commit on the dev branch which the feat branch comes from. An empty commit when you first make the dev branch helps establish the dev branch as it's own indefinitely lasting branch independent of master. Generally it's helpful when you use branches as layers, and create two layers from a single commit
  • Andy J
    Andy J over 7 years
    Another reason: If you omit something of importance out of your commit message before pushing, you cannot do commit --amend if the remote does not allow force pushing. This way you can allow developers to see an important message that goes with the previous commit.
  • sytech
    sytech almost 7 years
    I want to do this because I pushed a commit, but forgot to mention something in the commit message. Our commit messages are integrated with issue tracking and continuous integration software and the contents of the commit message affect those applications. Is there a better way? This seems like the best solution in my case. The only thing I can imagine would be somehow being able to revert the previous commit.
  • yoyodyn
    yoyodyn over 5 years
    I just used this to trigger our pre-commit hook, which scripts the database. So there were changes, just git couldn't see them until after the script had run. Could have run it manually of course, but then that would run the script twice.
  • Cœur
    Cœur over 5 years
    Maybe not a fantastic reason, but also to create a Pull request on GitHub to contact the owners when simply opening an Issue was disabled.
  • SimonF
    SimonF about 5 years
    Very useful when interacting with issue tracking builtin to git servers as github and gogs.
  • romulusnr
    romulusnr over 4 years
    When you're using an integrated PMS and you forgot to put the work item number in previous pushed commits but you want the PMS to track the commit against the work item.
  • romulusnr
    romulusnr over 4 years
    Yes there's commit --amend but that's a nightmare when the previous commit is already pushed.
  • Ryan Jendoubi
    Ryan Jendoubi almost 4 years
    Used to mark transitioning "master" branch to "main" branch; see e.g. this link. Just one example of using blank commits to capture significant changes to the repository itself which are not captured by code versioning.
  • Abdul Rauf
    Abdul Rauf almost 3 years
    allow-empty-message flag prompts for commit message. To skip that prompt we can pass empty mess using -m "" like git commit --allow-empty --allow-empty-message -m ""
  • ecv
    ecv almost 3 years
    One reason could be: Since the last commit I've been sleeping and now I am marking the actual time I start working (with a message of "nop", as in no op) in the next commit so you could tell how long it took me actually to make the changes.