DEV Community

Simc Dev
Simc Dev

Posted on • Originally published at cmsinstallation.blogspot.com

How to Push an empty commit?

Have you ever tried to push a commit to a Git branch without changing any files in order to re-run your integration process? If yes then you are at the right corner.

git commit --allow-empty -m β€œMessage”
Enter fullscreen mode Exit fullscreen mode

Problem

For continuous integration, we are using AWS CI/CD delivery pipelines which allow us to build, test and deploy applications on a single push to a specific git branch. It helps us to reduce the manual overhead of deploying code to the server and handle all the actions automatically.

But today I faced a problem where I needed to re-run my delivery pipeline of a branch without adding any extra space or changing any files in the repository, so I searched for the solution for a while and It turns out that Git is allowing us to push an empty commit without adding any staged files to the branch, by using one option --allow-empty during git commit.

Enough of the problem, let’s jump on to the solution
Pushing a commit with staged files

git add .

git commit -m "changes on app controller"

git push origin master 
Enter fullscreen mode Exit fullscreen mode

The above commands will add all unstaged files and add commit and push the code to the master branch, after that our delivery pipeline will be started.

Once the pipeline process fails or you need to run the process again, you will have to push something to the branch but as I mentioned earlier, we will not make any changes to the files, and even then, We will be able to commit the branch with this command.

Pushing empty commit

git commit --allow-empty -m "rerunning the delivery pipeline"

git push origin master 
Enter fullscreen mode Exit fullscreen mode

After the above commands, you can see that the commit has been pushed to your branch and the delivery pipeline will be started.

Top comments (10)

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡ • Edited

I don't know which thingy are you using but thinking of gitlab-ci, jenkins, rundeck... Or anything else I've ever saw, I really need to ask:

Why didn't you just go to the dashboard and hit the "Run job" button instead? πŸ˜…πŸ˜…πŸ˜…

Collapse
 
danwalsh profile image
Dan Walsh

I use this command all the time to do things like rebuild and redeploy sites on Netlify. While you can definitely just use the UI in Netlify to trigger the same work with one click, using the empty commit command does two great things:

  1. It allows me to log a custom message as to why a rebuild was triggered, and
  2. It confirms that my entire pipeline is working all the way from the terminal commands right through to deployment. Clicking the redeploy button in the Netlify UI only tests part of the automation pipeline.
Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡

Just out of curiosity, which are the reasons to perform a rebuild of the same code that's already been deployed?

Sorry if it's a dumb question in this context I've never used netlify.

Thread Thread
 
danwalsh profile image
Dan Walsh

Definitely not a dumb question!

Often it's when I've made some sort of configuration change within Netlify (or some other part of the pipeline that exists outside of the repository). While there is no actual "code change" to track, I'm still able to record a commit message using the --allow-empty flag for record keeping purposes.

For example:

~ git commit --allow-empty -m "chore: swapped Netlify secrets to prod env values"
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
joelbonetr profile image
JoelBonetR πŸ₯‡ • Edited

Oh so there's no real need for re-deploying, you just need to restart the App so it gathers the new env, but this way you keep track of those changes inside the same repo through comments.

What we do is to keep a separate repo for this DevOps/sysadmin stuff so we get the history on that topic isolated from the application repository. It works well in GCP, AWS... But maybe those changes are directly inside Netlify in your specific case?

Thread Thread
 
danwalsh profile image
Dan Walsh

Yeah, those changes can be made directly inside Netlify's UI. The secret swapping was just an example. Other instances might be to test a newly implemented git hook that was added to the pipeline, or testing Netlify configuration changes to build caching.

I normally use this approach for much smaller projects (e.g. statically generated sites) where having everything in one place is manageable. I certainly see the merit is splitting that out to a separate branch (or repository even) for projects at larger scale.

Thread Thread
 
organizedfellow profile image
Jaime Aleman

"go": "git commit --allow-empty -m 'DEPLOY!' && netlify deploy --build --prod"
I have that in package.json and do npm run go.

Sometimes I just don't want to push to repo as I may have untracked/uncommited files.

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman
git push -f
Enter fullscreen mode Exit fullscreen mode
Collapse
 
naucode profile image
Al - Naucode

That was a good read, thank you, followed and bookmarked!

Collapse
 
devsimc profile image
Simc Dev

Cheers