skip to content
Ruban Selvarajah

Git Timetravel: Setting Custom Dates for Your Commits

/ 2 min read

git

Have you ever wished you could travel back in time with your Git repository? While we can’t bend the laws of physics, we can certainly tweak the timestamps of our Git commits! This can be particularly useful for project organization, correcting date errors, or simply maintaining a cleaner, more logical commit history.

Let’s 🚀

Understanding Git Date Manipulation

Git, the widely-used version control system, allows you to specify dates for your commits. This can be done using the GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables. By setting these variables, you can make a commit appear as though it was made on a different date.

How to Set Custom Commit Dates

Step 1: Determine Your Desired Date

First, decide on the date and time you want to set for your commit. This can be any past or future date. For example, let’s say you want to date the commit to January 1, 2023, at 10:00 AM.

Step 2: Format the Date

Use the date command to format this date properly. In our example, it would be:

Terminal window
date -d'2023-01-01 10:00:00'

Step 3: Set the Environment Variables

Before making your commit, set the GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables to your formatted date. Here’s how you do it:

Terminal window
GIT_AUTHOR_DATE=$(date -d'2023-01-01 10:00:00') GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"

Step 4: Make Your Commit

Now, proceed with your git commit as usual:

Terminal window
git commit -m 'Your commit message'

Why and When to Use This

Manipulating the commit date can be useful in various scenarios:

  • Correcting Date Errors: If your system’s clock was wrong, and you want your repository to reflect the correct date.
  • Organizing Commits: For a cleaner, more logical order in your commit history.

A Word of Caution

While this trick is handy, use it responsibly. Altering commit dates in a collaborative project can lead to confusion. It’s best used in personal projects or for correcting genuine mistakes.


And there you have it! You now know how to set custom dates for your Git commits. This can be a powerful tool in your development arsenal, but remember to use it wisely to maintain the integrity and clarity of your project’s history.

Looking for more Git tips and tricks? Check out my previous post where I describe what I did when Git said my branch has diverged. Stay savvy and keep coding! 🚀