I would like to commit in a specific date in the past using JGit in Java. For example, I would like to commit to the local repository in 2023-03-01, 2023-03-02, 2023-03-03, 2023-03-04. According to many websites (including StackOverflow posts) I can achieve this by:
String oldDate = "2023-03-01";
PersonIdent author = new PersonIdent("X", String.valueOf(oldDate));
PersonIdent committer = new PersonIdent("X", String.valueOf(oldDate));
git.commit().setAuthor(author).setCommitter(committer).setMessage("Modified temp.txt").call();
However, this doesn't do what I want since the date stays the same, the actual date, coming from the system. I have checked the result using git log
in Git Bash: (Please check this image)
How can I do commits with past dates? All help is appreciated!
The PersonIdent
constructor you're calling in
new PersonIdent("X", String.valueOf(oldDate));
is
public PersonIdent(String aName, String aEmailAddress)
Construct a new PersonIdent with current time.
Emphasis mine. It looks like your specifying the email address instead of the time.
To specify the time, you'll need to use one of the four other non-copy/config constructors (as of JGit 6.5.0 2023-03-07), which are
public PersonIdent(String aName,
String aEmailAddress,
Instant aWhen,
ZoneId zoneId)
public PersonIdent(String aName,
String aEmailAddress,
ProposedTimestamp when)
public PersonIdent(String aName,
String aEmailAddress,
Date aWhen,
TimeZone aTZ)
public PersonIdent(String aName,
String aEmailAddress,
long aWhen,
int aTZ)