While working on your project using Git, a situation may occur when you need to test the changes you made. In order to do so, you’ll need to commit something to the repository. But, what should you do in case you need to trigger a new Continuous Integration build, and there are no new changes to commit?
While you have the option to go into your build provider and trigger a new one, is there an easier way? Also, depending on the environment, you might not have this option at all. Although the code may be open-source, you may not have any control over the build server. So, is there an easy way to redeploy your application to the remote server if there are no local changes you can push?
To answer these questions, our web development agency in Chicago reveals how to push an empty commit in Git.
What is an empty commit?
An empty commit is a commit without any changes. However, keep in mind that these commits will still be displayed in your history.
Why would you need to Git push an empty commit?
Sometimes you need to start a new build without making any changes to it. Or, as we have already said, you may not have an option to initiate the build manually. In this case, the only way to start a build is to use Git to push an empty commit.
However, it is vital to create a relevant message for your empty commit so other team members would understand the reason why such a commit exists in history.
How to push an empty commit in Git?
Pushing an empty commit without adding any staged files to the branch is very easy. It is the same as pushing a regular commit, except that all you need to do is add –allow-empty flag to the command line.
So, open up a terminal of your choice and type in the following:
git commit –allow-empty -m “[EMPTY] Your commit message here”
The reason we added this [EMPTY] prefix is simply to outline that this is an empty commit in case we need to remove it in the later stages. The next step is to push this commit to the main directory with:
git push origin main
The commit is now pushed to your branch without any changes.
Example: Re-running delivery pipeline of a branch without making changes to files in the repository
First, let’s explain what a delivery pipeline is:
Delivery Pipeline is part of the IBM Cloud Continuous Delivery service. It automates the continuous deployment of a project. In our case, it allows us to build, test, and deploy applications through a single Git push to a specific branch.
Using CI/CD pipelines is a widespread practice in modern software development. The way this process works is that you set up a webhook in your Git repository that allows deployments to run each time you push a commit. So, in case the deployment doesn’t get triggered, or if you simply need to re-trigger it for whatever reason, the first thing that comes to your mind could be to make a random change to a file in the repository, commit it, and push it onto the branch on the remote.
While you will undoubtedly trigger the deployment this way, we don’t feel that making unnecessary modifications is the professional way to go. That is where empty Git commits enter the scene. Let’s check how it works through an example.
We’ll start by pushing a commit with staged files:
git add .
git commit -m “app changes”
git push origin master
What we did here is added all unstaged files along with commit and pushed the code to the master, thus starting our delivery pipeline. Once the process fails, we want to re-run it. To do so, we need to push something to the branch without making any changes to the files.
So, let’s re-run the process by pushing an empty commit in Git:
git commit –allow empty -m “rerunning the process”
git push origin master
With the help of these commands, we have pushed the commit to our branch, and the delivery pipeline should start.
You can also shorten the entire process by employing Git aliases.
As a developer, you are certainly well aware of how much of a tedious task it is to repeat the same commands repeatedly. Git aliases allow you to shorten the command line and increase productivity by dedicating shortcuts to frequently used Git commands.
For example, instead of typing git commit -m “your message” each time you need to commit changes, you can create an alias like git cm “message.” While this may not sound like a significant improvement initially, once you repeat the same process countless times, you will undoubtedly begin to appreciate Git aliases.
To set up Git alias, you need to run git config –global alias. followed by the shortcut as well as the full Git command. If this sounds a bit complicated, we hope this template will clarify things:
git config – global alias.[YourShortcut] [gitCommand]
You run aliases the way you would run any other Git command. Which shortcuts you dedicate to which commands is entirely up to you. Still, to avoid any confusion, we suggest you come up with shortcuts that match and describe the commands in question. For example, an alias for git checkout master could look something like git cm, and so on.
As you can see, empty commits are mainly used for deployment purposes and can be a very valuable feature to add to your repertoire of Git commands. Pushing commits in Git, whether empty or not, will cause eventual Git hooks to be triggered. It is much wiser to use an empty commit in Git to trigger the desired action than to make some insignificant changes to files, such as adding spaces to a Read Me file just to be allowed to make a commit.
This is where we’ll wrap up our article—just one more thing before we say goodbye. Feel free to schedule a call with our agency in case you need any help with your project development. We also do custom-coded websites that can help your company increase its online visibility, thus putting your products or services within reach of your potential customers.