How do I make a Git commit in the past?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git stores both an author date and a committer date for each commit, and you can set either one to an earlier timestamp if you really need to. The mechanics are simple, but rewriting commit dates has consequences for trust, collaboration, and history rewriting, so it should be done deliberately rather than casually.
The Two Dates Git Tracks
Every commit records:
- '
GIT_AUTHOR_DATE, which represents when the author originally created the change' - '
GIT_COMMITTER_DATE, which represents when the commit object was actually written'
In normal everyday commits, those values are the current time. To make a commit appear in the past, set both environment variables before running git commit.
Create a New Backdated Commit
Here is the standard one-off pattern:
That creates a brand-new commit whose author and committer timestamps both point to January 15, 2024 at 10:30 in the -0500 time zone.
You can inspect the result with:
The fuller format is useful because it shows both dates explicitly.
Amend an Existing Commit's Date
If the commit already exists and you want to rewrite the most recent one, amend it:
--date sets the author date. The GIT_COMMITTER_DATE environment variable sets the committer date. Without both parts, the two timestamps may differ.
Rewriting Older Commits
If the commit you want to change is not the latest one, you need history rewriting, usually through interactive rebase.
The common flow is:
- Start an interactive rebase covering the commit.
- Mark that commit for edit.
- Amend the commit with the desired dates.
- Continue the rebase.
For the amend step, the command is the same pattern:
This rewrites commit hashes, so anyone sharing the branch with you will need to reconcile their history afterward.
Accepted Date Formats
Git accepts several date formats, but ISO 8601 style is the safest and clearest:
Using an explicit time zone avoids ambiguity. Relative forms such as "2 weeks ago" also work, but exact timestamps are better when you want reproducible history.
Why People Do This
Legitimate reasons exist. You may be importing a historical patch, preserving authorship information from another tool, or reconstructing internal history during a repository migration.
That said, backdating commits to make activity look different than it really was can create trust problems. In team environments, rewritten timestamps should have a clear operational reason.
Shared Repositories Need Extra Care
If the commit has already been pushed, changing its date means changing the commit hash. To update the remote branch, you would typically need a force push:
That is safe only when you understand the branch state and who else is using it. On protected branches, it may not even be allowed.
Common Pitfalls
The first pitfall is setting only the author date and forgetting the committer date. Then git log --pretty=fuller shows mismatched timestamps.
Another pitfall is rewriting history on a shared branch without warning. Even a tiny metadata change produces new commit hashes.
A third pitfall is using vague date strings without a time zone. That makes the result harder to reason about across machines and collaborators.
Finally, do not confuse commit dates with file timestamps. Backdating a commit changes Git metadata, not the file system modification time on disk.
Summary
- Use
GIT_AUTHOR_DATEandGIT_COMMITTER_DATEto create a commit with a past timestamp - Use
git commit --amend --date ...plusGIT_COMMITTER_DATEto rewrite an existing commit - Prefer explicit ISO-style timestamps with time zones
- Rewriting dates changes commit hashes when the commit already exists
- Be careful on shared branches because backdating commits is a history rewrite
Related reading
- How do I make git-svn use a particular svn branch as the remote repository?
- How do I make Git forget about a file that was tracked, but is now in .gitignore?
- How do I make Git ignore file mode chmod changes?
- How do I make git use the editor of my choice for editing commit messages?
- How do I merge changes to a single file, rather than merging commits?
- How do I merge my local uncommitted changes into another Git branch?
- How do I merge two dictionaries in a single expression in Python?
- How do I migrate an SVN repository with history to a new Git repository?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.