Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the commit time with JGit?

Tags:

java

commit

jgit

Is there a way to set the commit time with JGit?

I flipped through the API and found that it can only be done by modifying the local system time. I want to implement it through code. And can run normally on the win system.

like image 975
xiao luo Avatar asked Jul 03 '18 09:07

xiao luo


People also ask

How do I change commit time?

Just do git commit --amend --reset-author --no-edit . For older commits, you can do an interactive rebase and choose edit for the commit whose date you want to modify.

Does Git commit have timestamp?

There are actually two different timestamps recorded by Git for each commit: the author date and the commit date. When the commit is created both the timestamps are set to the current time of the machine where the commit was made.

How do I hide commit time on github?

This API data stored directly in their database, not in the Git commit data, and there is no way to fake it. If you want to hide that, there is no alternative AFAIK, you have to: push at a different time e.g. with a cron job. delete the repository, then the push data goes away from the API.


2 Answers

The timestamp for a commit can be set with the CommitCommand. Note that name, email, and timestamp must be specified together with a PersonIdent object.

For example:

Date date = ...
PersonIdent defaultCommitter = new PersonIdent(git.getRepository());
PersonIdent committer = new PersonIdent(defaultCommitter, date);
git.commit().setMessage("Commit with time").setCommitter(committer).call();

The defaultCommitter holds name and email as defined in the git config, the timestamp is the current system time. With the second PersonIdent constructor, the name and email are taken from the defaultCommitter and the timestamp is overridden with date.

like image 96
Rüdiger Herrmann Avatar answered Sep 19 '22 15:09

Rüdiger Herrmann


In windows the system time can be set by executing the command 'date MM-dd-yy' from the and 'Administrator' command prompt.

Java Snippet for Windows

 //Set the Date 
 SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy");  
 String setDate = "cmd /C date "+sdf.format(dateToSet);  
 Process dateProc = Runtime.getRuntime().exec(setDate);  
 dateProc.waitFor();//Might take a couple of seconds

 //Set the Time  
 SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");  
 String setTime = "cmd /C time "+stf.format(dateToSet);  
 Process timeProc = Runtime.getRuntime().exec(setTime);  
 timeProc.waitFor();//Might take a couple of seconds  

This command can only be executed as an administrator. So you should run the java code with Administrator Privileges.

like image 44
CrackTheLogic Avatar answered Sep 18 '22 15:09

CrackTheLogic