Commit Without Files
I was recently looking into whether it was possible to write commit messages without actually commiting files. Come to find out, it is possible. You're probably thinking, why would one want to do this? There are couple reasons that I could come up with.
One of the most common reasons to do this is so that you can trigger the CI/CD workflow after a failed run. In some situations, a run may fail for reasons other than the code. For instance, a server may not be responding or is offline.
You're probably saying, just go in the workflow tool (Jenkins, DevOps, GitHub Actions) and manually trigger the work flow. In some set ups, only certain individuals are allowed to trigger workflows manually, such as system admins or team managers. Access control is a big thing, especially in larger organizations.
Updating Issues / User Stories
In my case, I have been trying to be more proactive about closing the GitHub Issues with commit messages instead of using comments on the issues themselves. Why is that the case? Because it associates the issue with the commit. Having the issue associated with the commit is helpful when tracing when bugs or defects were introduced into the code.
How To Commit Without Files
If you try to do a commit without adding files first, then you will get the below output:
almostengineer@aeoffice:~/ubuntu-automation/git_scripts$ git commit -m "my commit message" On branch main Your branch is up to date with 'origin/main'. nothing added to commit but untracked files present (use "git add" to track)
As you see, git will tell you that there is nothing to track and thus nothing to commit to the repository.
Normally when you do a commit, you run
git add followed by
git commit. However, since there are no files to commit, you skip the
git add command and only do the commit.
Here's the command that you run to do a commit without any files:
git commit --allow-empty -m "My commit message"
Let's break down the command. The
git commit and
-m "My commit message" are familiar to you if you do your commits via the command line like how I do mine.
--allow-empty is probably what you are not familiar with. This part is the argument that has to be passed in so that you can do a commit without any files.
Looking at the documentation with "git commit --help", it says
--allow-empty Usually recording a commit that has the exact same tree as its sole parent commit is a mistake, and the command prevents you from making such a commit. This option bypasses the safety, and is primarily for use by foreign SCM interface scripts.
As you see, this is why when you try to do a commit without adding files first, that you get the error message mentioned above.
I plan on using this going forward when I miss closing issues or user stories with previous commits. I probably will not being using it to trigger workflows as I have access to my personal CI/CD pipeline tools and can trigger them manually when need be.