One of the fundamental features of Git is the ability to manage individual metadata for its various objects like commits. One of the most important pieces of metadata is the date associated with the commit, or the "commit date".
This guide explores the details of Git commit dates, including how to view, change, and manipulate these dates to suit various development needs.
Understanding Git commit dates
In Git, each commit is associated with two main types of dates:
- Author date: This is the date when the changes were originally made.
- Commit date: This date reflects when the changes were last applied or altered in any significant way, such as during a rebase or amend.
These dates help maintain the integrity and chronological order of changes in the repository.
Viewing the commit date of a specific commit
To view the dates associated with a specific commit, use the git show
command:
git show --pretty=fuller COMMIT_HASH
Replace COMMIT_HASH
with the actual hash of the commit you are interested in. This command displays both the author date and the commit date, along with other commit information.
Below is an example of the type of output you might see after running this command:
commit 1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t (HEAD -> main, origin/main, origin/HEAD)Author: Alice Johnson <alice@example.com>AuthorDate: Tue Mar 14 15:26:47 2023 -0400Commit: Bob Smith <bob@example.com>CommitDate: Wed Mar 15 16:35:12 2023 -0400Added a new feature to improve performancediff --git a/file1.txt b/file1.txtindex 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
commit 1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t (HEAD -> main, origin/main, origin/HEAD)
:- This line starts with the word
commit
followed by the commit hash (1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t
). The hash uniquely identifies this commit in the Git repository. (HEAD -> main, origin/main, origin/HEAD)
shows the branch pointers:HEAD
is pointing tomain
, indicating you are currently on themain
branch.origin/main
andorigin/HEAD
indicate the state of the main branch on the remote namedorigin
.
- This line starts with the word
Author: Alice Johnson <alice@example.com>
:- This specifies who made the changes. "Alice Johnson" is the author, and
<alice@example.com>
is the email address associated with the author.
- This specifies who made the changes. "Alice Johnson" is the author, and
AuthorDate: Tue Mar 14 15:26:47 2023 -0400
:- The date and time when the author originally made the changes.
Commit: Bob Smith <bob@example.com>
:- This line indicates the person who actually committed the changes to the repository. In Git, the committer may be different from the author if someone else commits the author's changes, such as when changes are pulled from a patch or another branch.
CommitDate: Wed Mar 15 16:35:12 2023 -0400
:- The date and time when the commit was actually made to the repository.
The commit message:
"Added a new feature to improve performance"
is the description of what was changed in this commit, written by the author or committer.
diff --git a/file1.txt b/file1.txt
:- This line starts the diff section, showing changes between the previous version of
file1.txt
and the new version in the current commit.
- This line starts the diff section, showing changes between the previous version of
index 1234567..89abcde 100644
:- Shows the file mode and the blob hashes before and after the commit for
file1.txt
.
- Shows the file mode and the blob hashes before and after the commit for
--- a/file1.txt
and+++ b/file1.txt
:- Indicates the old file (
---
) and the new file (+++
) being compared.
- Indicates the old file (
@@ -1,3 +1,5 @@
:- This line in the diff output is a hunk header. It tells you which lines are being changed in the file. The format is
@@ -start,length +start,length @@
. The numbers after the minus sign refer to the old file, and the numbers after the plus sign refer to the new file.
- This line in the diff output is a hunk header. It tells you which lines are being changed in the file. The format is
The changes in content:
- Lines beginning with
-
are lines that were removed, and lines beginning with+
are lines that were added. In this case:-Old line of code
shows a line that was removed.+New line of code
and+Another new line of code
show lines that were added.
- Lines beginning with
This output gives a comprehensive view of what happened in this commit, including who made the changes, when they were made, and exactly what those changes were in the context of the file(s) involved.
Changing commit dates
Manipulating commit dates can be crucial for various reasons, such as correcting an incorrect system time during a commit or aligning commits in a logical order after a rebase.
Amend the most recent commit date
To change the date of the most recent commit, you can use the
git commit --amend
command along with environment variables to set new dates.Change to the current date and time:
TerminalGIT_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.
Keep the original author date:
TerminalGIT_COMMITTER_DATE="$(date)" git commit --amend --no-editThis only changes the commit date to the current date, keeping the original author date.
Change the date of any commit
To change the date of any commit, not just the most recent one, you will need to use an interactive rebase:
Terminalgit rebase --interactive 'commit^'Replace
commit
with the hash of the commit you want to modify. Mark the commit withedit
in the list that appears. When Git pauses at the commit, change the date:- Set a specific date:ReplaceTerminalGIT_COMMITTER_DATE="YYYY-MM-DD HH:MM:SS" git commit --amend --date "YYYY-MM-DD HH:MM:SS"
YYYY-MM-DD HH:MM:SS
with the desired date and time.
After amending the commit, continue the rebase with:
Terminalgit rebase --continue- Set a specific date:
Git commit date formats
Git supports a variety of date formats when you're working with commits, making it highly adaptable to different workflows and situations. This flexibility is especially useful when filtering commits by date or setting dates manually.
Standard date format
The standard format that Git displays dates is typically in the ISO 8601-like format: YYYY-MM-DD HH:MM:SS
. This format includes the year, month, day, hour, minute, and second, providing a precise timestamp for each commit. This format is used in Git log outputs by default, unless specified otherwise using formatting options.
Custom date formats
Git also allows you to customize how dates are displayed in log messages by using the --date=format
option with git log
. For example:
git log --date=format:'%Y-%m-%d %H:%M:%S'
This command will format the dates in the log to the specified format, which in this case is the standard format. However, you can customize this to include or omit details as needed:
git log --date=format:'%Y-%m-%d' # Only display year, month, and day
Human-readable dates
Git can interpret human-readable date strings when specifying dates for commands, thanks to its integration with the system's date parsing libraries. This feature is particularly useful for operations like filtering logs within a certain timeframe. Examples of human-readable formats include:
Relative dates: Dates like "2 weeks ago", "yesterday", "3 months ago", or "last Monday" can be used in various Git commands. For instance, if you want to see commits from the last two weeks, you might use:
Terminalgit log --since="2 weeks ago"Specific dates without times: You can specify dates without times in a more casual format, such as "April 1", "2023-04-01", or "01-Apr-2023". These are interpreted contextually based on the current time.
Practical tips and considerations
- Implications of changing commit dates: Modifying commit dates can be useful for cleaning up a commit history before merging branches. However, it should be done with caution, as it alters the historical record of the repository.
- Maintaining chronology: Always ensure that the commit dates maintain a logical chronology that reflects the actual development and collaboration process.
For further reading on the timestamps and other metadata Git commits contain, see the official documentation.