How to commit no change and new message?
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.
Related videos on Youtube
Comments
-
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 almost 8 yearsI've started to follow the git flow branch model. When you make a
dev
branch formmaster
and then afeat
branch immediately fromdev
, thefeat
branch looks to come from themaster
branch as there is no distinguishing commit on thedev
branch which thefeat
branch comes from. An empty commit when you first make thedev
branch helps establish thedev
branch as it's own indefinitely lasting branch independent ofmaster
. Generally it's helpful when you use branches as layers, and create two layers from a single commit -
Andy J over 7 yearsAnother 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 almost 7 yearsI 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 over 5 yearsI 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 over 5 yearsMaybe 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 about 5 yearsVery useful when interacting with issue tracking builtin to git servers as github and gogs.
-
romulusnr over 4 yearsWhen 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 over 4 yearsYes there's commit --amend but that's a nightmare when the previous commit is already pushed.
-
Ryan Jendoubi almost 4 yearsUsed 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 almost 3 years
allow-empty-message
flag prompts for commit message. To skip that prompt we can pass empty mess using-m ""
likegit commit --allow-empty --allow-empty-message -m ""
-
ecv almost 3 yearsOne 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.