Graphite Reviewer is now Diamond

Understanding Git commit dates

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Git allows developers to track changes and manage projects efficiently. A critical aspect of this management involves using metadata associated with Git objects, such as commits. Changing commit dates in Git can be useful for correcting errors, such as wrong system time settings, maintaining chronological order during rebase or merge operations, and standardizing time zones across global teams. This guide explains the commit date and how to view, modify, and manipulate these dates effectively.

In Git, each commit is linked to two primary types of dates:

  • Author date: The date when the original changes were made.
  • Commit date: The date when the changes were last significantly modified, for example, during a rebase or amend operation.

These dates are essential for maintaining the integrity and chronological order of changes within the repository.

To view the dates associated with a specific commit, the git show command is indispensable:

Terminal
git show --pretty=fuller COMMIT_HASH

Replace COMMIT_HASH with the actual hash of the commit you want to investigate. This command will display both the author and commit dates, among other details.

Terminal
commit 1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t (HEAD -> main, origin/main, origin/HEAD)
Author: Alice Johnson <alice@example.com>
AuthorDate: Tue Mar 14 15:26:47 2023 -0400
Commit: Bob Smith <bob@example.com>
CommitDate: Wed Mar 15 16:35:12 2023 -0400
Added a new feature to improve performance
diff --git a/file1.txt b/file1.txt
index 1234567..89abcde 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,5 @@
-Old line of code
+New line of code
+Another new line of code

For various reasons, such as correcting system time errors during a commit or rearranging commits logically after a rebase, you might need to adjust commit dates.

Terminal
GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

This command sets both the author date and the commit date to the current system date and time.

Terminal
GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit

This modifies the commit date while preserving the original author date.

For changing the date of any commit, not just the most recent:

Terminal
git rebase --interactive 'commit^'

Replace commit with the hash of the desired commit, mark it with edit when prompted, and modify the date when Git pauses:

Terminal
GIT_COMMITTER_DATE="YYYY-MM-DD HH:MM:SS" git commit --amend --date "YYYY-MM-DD HH:MM:SS"

After amending, continue the rebase with:

Terminal
git rebase --continue

The default display format for Git dates is similar to the ISO 8601 standard: YYYY-MM-DD HH:MM:SS, offering precise timestamps.

Customize how dates appear in logs:

Terminal
git log --date=format:'%Y-%m-%d %H:%M:%S'

Git supports human-readable date strings, which are helpful for filtering logs or specifying dates in commands:

Terminal
git log --since="2 weeks ago"

Modifying commit dates can be crucial for maintaining a clean history before merges but should be approached with caution to avoid altering the repository's historical integrity. Always ensure that commit dates reflect the actual development and collaboration chronology.

For more details on Git's timestamps and metadata, refer to the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone