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?
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 it will definitely have a different parent commit. All of those factors are integrated into git
's object hash algorithm (among others like author and committer).
Empty commits become a part of the git commit graph just like regular commits, so be careful if using --allow-empty
for ephemeral usage like triggering build scripts or "scratch pad" notes in large projects. Those commits may add noise to the git history or may make it more difficult for you or your collaborators to find more-relevant commits later—your colleagues may not expect to download your build script invocations every time they pull your repo. For local development this might be a reason to label ephemeral empty commits consistently (perhaps using -m
) and remove them pre-merge with git rebase -i
.
That said, to incorporate some of the comments, you may want long-lived empty commits for some of these reasons:
--amend
the commit, particularly when force-pushes are disallowed (via Andy J).dev
from its first feature (via Novice C).master
to main
naming (via Ryan Jendoubi) or milestones in feature development (via NeilG).And also some ephemeral ones:
git
commands without generating arbitrary changes (via Vaelus).gitolite
(via Tatsh).Other strategies to add metadata to a commit tree include:
git notes
to associate a mutable note on top of an existing immutable commit.Empty commit with a message
git commit --allow-empty -m "Empty test commit"
Empty commit with an empty message but you will be asked to type the message
git commit --allow-empty --allow-empty-message
Empty commit with an empty message
git commit --allow-empty --allow-empty-message -m ""
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 ""
Commented
Aug 31, 2021 at 9:48
If I understood you right, you want to make an empty commit. In that case you need:
git commit --allow-empty
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.
Maybe as a more sensible alternative, you could create an annotated tag (a named commit with a message). See the git tag -a
option.