Git: How to commit and push unchanged files (to trigger some actions)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Git is a distributed version control system popular among programmers for its ability to track changes in source code during software development. However, there might be occasions when you need to commit and push files that haven’t changed to trigger certain actions like a Continuous Integration (CI) or Continuous Deployment (CD) pipeline. This article covers several ways to force Git into accepting and pushing an unchanged commit.

Prerequisites

  • Basic understanding of Git commands.
  • A configured and initialized Git repository.

Basic Commit without Any Changes

If you try to commit without changes, Git will not permit it, because there are no changes to commit.

$ git commit -m 'Trigger CI'
on branch main
Your branch is up-to-date with 'origin/main'.
nothing to commit, working directory clean

Using touch Command

This method involves updating the timestamp of a file without changing its contents. The touch command is used to update the file modification time, which Git sees as a change.

$ touch trigger.txt
$ git add trigger.txt
$ git commit -m 'Trigger empty commit'
[master 0e4db9b] Trigger empty commit
 1 file changed, 0 insertions(+), 0 deletions(-)
$ git push origin master

Empty Commit

A straightforward approach is to create an empty commit with the --allow-empty option. This makes Git generate a commit that has no content changes.

$ git commit --allow-empty -m 'Trigger CI with empty commit'
[master 123a456] Trigger CI with empty commit
$ git push origin master

Advanced Tactics to Trigger Actions

For more complex situations, additional steps may be required.

Forcing a Non-changes Commit with --amend

A commonly used trick is to amend the last commit without actually changing it and then force push to the remote.

$ git commit --amend --no-edit
$ git push --force-with-lease origin master

Changing and Reverting a File

Sometimes two commits are used: one that introduces a change and one that reverts it.

$ echo 'temporary change' >> file.txt
$ git add file.txt
$ git commit -m 'Temporary change to trigger CI'
$ git revert HEAD --no-edit
$ git push origin master

Creating a Git Alias for Empty Commits

You can create an alias in Git to simplify running the command each time.

$ git config --global alias.empty '!git commit --allow-empty -m'
$ git empty 'CI trigger commit'
$ git push origin master

Working with Tags

Sometimes triggering an action is not about changes in the files but changes in tags.

Adding and Pushing Tags

You can create a new tag and push that tag to trigger a CI/CD process.

$ git tag -a v1.2.3 -m 'Trigger deploy with tag'
$ git push origin v1.2.3

Handling Potential Pitfalls

A thorough approach also addresses the potential problems of using these tactics.

Being Conscious of CI/CD Costs

Every time a CI/CD pipeline runs, it consumes resources. Frequent triggering without code changes should be done considering the costs.

Maintaining the Repository’s Integrity

Pushing empty commits or using –force commands might cause confusion or conflict within the team. Clear communication on the use case is essential.

Security Implications of --force Push

Overwriting commit history can sometimes lead to security implications or loss of work and must be done with care.

Conclusion

In summary, there are various methods to commit and push unchanged files in Git, from simple commands like touch to more complex strategies involving empty commits or tag manipulation. When using these methods, be mindful of the impact on CI/CD workflows and communicate effectively with your team to maintain repository integrity.