How to tweak Git commit timestamps (author and committer dates)

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

Introduction

Git is a distributed version control system widely used for tracking changes in source code during software development. While Git keeps thorough records, including timestamps for when commits are made, there are instances where you might need to alter these timestamps. Perhaps you want to correct a commit that was made with the wrong system time or you might want to edit the timestamp of a commit for some other legitimate reason. This tutorial will guide you through the process, from the simplest methods to the more complex and less commonly used techniques.

Understanding Git Timestamps

Before we dive into tweaking timestamps, it’s important to understand what they are. Git records two timestamps for each commit:

  • Author Date: The date and time when the changes were originally made.
  • Committer Date: The date and time when the commit was actually applied to the repository.

By default, these are usually the same but can differ, for example, when patches are applied later than they were originally written.

Basic Timestamp Tweaking

The simplest way to tweak a timestamp is during the commit process itself. This can be performed by setting the GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables before executing the git commit command.

export GIT_AUTHOR_DATE="2023-04-01T12:00:00"
export GIT_COMMITTER_DATE="2023-04-01T12:00:00"
git commit -m "Commit with a custom timestamp"

You can specify the date in any format that Git recognizes, such as RFC 2822 or ISO 8601. An example output might look like this:

$ git log --format=fuller
commit 5a11431edba2e3ff764b23ee5e39787d5cd09807
Author:     John Doe <[email protected]>
AuthorDate: Sat Apr 1 12:00:00 2023 +0000
Commit:     Jane Doe <[email protected]>
CommitDate: Sat Apr 1 12:00:00 2023 +0000

    Commit with a custom timestamp

Amending Commits with Different Timestamps

If you’ve already made a commit and you want to change its timestamps, you can use the git commit --amend command along with the aforementioned environment variables.

export GIT_AUTHOR_DATE="2023-04-02T12:00:00"
export GIT_COMMITTER_DATE="2023-04-02T12:00:00"
git commit --amend --no-edit

This amends the most recent commit without changing the commit message. The output of git log --format=fuller will show updated timestamps for that commit.

Advanced Timestamp Editing

Moving to more advanced scenarios, you might need to modify timestamps for commits that have not been pushed to a remote repository yet. Interactive rebase is a powerful tool that allows you to do this.

List all the commits you want to edit using:

git rebase -i HEAD~NUM

where ‘NUM’ is the number of commits you want to edit. Use ‘edit’ in the interactive window for the commits you want to change.

When you have stopped at the commit you want to edit, change the timestamp:

GIT_AUTHOR_DATE="2023-04-01T12:00:00" GIT_COMMITTER_DATE="2023-04-01T12:00:00" git commit --amend --no-edit

Then continue the rebase:

git rebase --continue

Repeat the steps for each commit you’ve marked as ‘edit’. Note that this modifies the commit hash and it’s not advisable to do this on commits that have been shared with others.

Using Filters to Mass Edit Commits

For more complex history rewrites where you want to tweak the timestamps on a series of commits, you can use the filter-branch command. WARNING: This is a very advanced and potentially destructive operation.

git filter-branch --env-filter '
OLD_DATE="old date"
NEW_DATE="new date in any recognized format"
if [ "$GIT_COMMITTER_DATE" = "$OLD_DATE" ]; then
    export GIT_AUTHOR_DATE="$NEW_DATE"
    export GIT_COMMITTER_DATE="$NEW_DATE"
fi
' HEAD~NUM..HEAD

This example changes every commit’s timestamps from ‘OLD_DATE’ to ‘NEW_DATE’ starting from ‘HEAD~NUM’ up to ‘HEAD’. This should be used with extreme caution and only after ensuring that the repository’s backup is created.

Conclusion

In this tutorial, we explored how to tweak git commit timestamps. We discussed multiple methods, ranging from simple command-line tricks to complex history rewriting commands. Regardless of why you need to modify commit timestamps, it’s crucial to proceed with caution, as these actions can alter the integrity of your git history, particularly in a collaborative environment.