Pushing empty commits to remote

Learn pushing empty commits to remote with practical examples, diagrams, and best practices. Covers git, git-commit development techniques with visual explanations.

Mastering Git: Pushing Empty Commits to Remote Repositories

Hero image for Pushing empty commits to remote

Learn why and how to create and push empty Git commits, a useful technique for triggering CI/CD pipelines or marking progress without code changes.

In Git, a commit typically represents a set of changes to your codebase. However, there are scenarios where you might need to record a commit that introduces no actual file modifications. These are known as 'empty commits.' While seemingly counterintuitive, empty commits serve specific purposes, especially in automated workflows and project management. This article will explore the reasons for using empty commits and provide clear instructions on how to create and push them to your remote repository.

Why Push an Empty Commit?

Empty commits are not a common daily practice, but they are invaluable in particular situations. Understanding these use cases helps in leveraging Git more effectively for complex development workflows.

Here are the primary reasons you might opt for an empty commit:

1. Triggering CI/CD Pipelines

Many Continuous Integration/Continuous Deployment (CI/CD) systems are configured to automatically run builds, tests, or deployments whenever new commits are pushed to a specific branch. If you need to re-run a pipeline for a branch without making any code changes (e.g., to re-deploy a static site, re-run tests after an infrastructure change, or update a dependency outside the repository), an empty commit provides the necessary push event.

2. Marking Progress or Milestones

Sometimes, a significant non-code-related event occurs that you want to record in your repository's history. This could be the completion of a design phase, a successful manual deployment, or a decision point. An empty commit with a descriptive message can serve as a historical marker.

3. Updating Branch Protection Rules

In some repository hosting services, certain branch protection rules might be tied to recent activity or require a push to re-evaluate. An empty commit can sometimes help in these edge cases, though this is less common.

How to Create and Push an Empty Commit

Creating an empty commit is straightforward using the Git command-line interface. The key is to use the --allow-empty flag with git commit.

flowchart TD
    A[Start] --> B{Need to trigger CI/CD or mark progress?}
    B -- Yes --> C[Run 'git commit --allow-empty -m "Your message"']
    C --> D[Run 'git push origin <branch_name>']
    D --> E[Empty commit pushed to remote]
    B -- No --> F[Continue with regular development]
    E --> G[End]
    F --> G

Workflow for creating and pushing an empty commit

1. Step 1: Navigate to Your Repository

Open your terminal or command prompt and navigate to the root directory of your Git repository.

2. Step 2: Create the Empty Commit

Use the git commit command with the --allow-empty flag. This flag tells Git to create a commit even if there are no changes staged. You must also provide a commit message using the -m flag.

3. Step 3: Push the Empty Commit

Once the empty commit is created locally, push it to your remote repository just like any other commit. Replace <branch_name> with the name of the branch you are working on (e.g., main or master).

git commit --allow-empty -m "Trigger CI/CD pipeline for infrastructure update"
git push origin main

Example of creating and pushing an empty commit to the 'main' branch.

Verifying the Empty Commit

After pushing, you can verify that your empty commit has been successfully added to the remote repository's history. You can do this by checking your repository's commit log on your Git hosting service (e.g., GitHub, GitLab, Bitbucket) or by using Git commands locally.

git log --oneline -1

View the most recent commit in your local history.

The output will show your empty commit with its message, confirming its presence in the history.