DEV Community

Konstantin
Konstantin

Posted on • Updated on • Originally published at konstantin.io

Refresh commit date

Imagine that you made some commits for which you would like to update the time or a date.(so that you would keep only the chronological order)

Now if you were to do it on the latest commit, you can just run:

GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
Enter fullscreen mode Exit fullscreen mode

and that would update the date of the last commit, so you can then push or push force it.

Multiple commits

To perform this action in bulk mode you can just run:

git rebase HEAD~{number of commits} --ignore-date
Enter fullscreen mode Exit fullscreen mode

You can also create a shortcut in your ~/.gitconfig like so:

[alias]
  refresh = "!f() { git rebase HEAD~$1 --ignore-date; }; f"
Enter fullscreen mode Exit fullscreen mode

where the parameter would be number of commits starting from HEAD to update, i.e.:

git refresh 6
Enter fullscreen mode Exit fullscreen mode

Top comments (8)

Collapse
 
jessekphillips profile image
Jesse Phillips

Could I get more reason why I'd want to rewrite chronological order?

Collapse
 
konstantin profile image
Konstantin • Edited

You don’t rewrite the chronological order. You rewrite timestamps but you keep the chronological order.

Collapse
 
lucaskunzler profile image
Lucas Kunzler

And why would I want to do that?

Thread Thread
 
puritanic profile image
Darkø Tasevski

Probably to keep GitHub activity streak or something. Those sweet internet points 🙂

Collapse
 
jessekphillips profile image
Jesse Phillips

Chronological is in the order in which events happened (it is in relation to time). The original time stamp is the chronological order.

Why would I want to change the time stamp?

Thread Thread
 
konstantin profile image
Konstantin

Like you said, the chronological order is an order of events relative to timescale, so if you change the initial time stamp, but the order stays the same but relative to a new timestamp, it doesn’t mean that you’ve changed the chronological order, you just switched the initial timestamp. The order is the same.

That being said, if you decided to take a sample of commits before recent commit/commits, and switched initial timestamp of the first commit of that sample so it comes after every commit, you would effectively change the chronological order, that is to say the sample would chronologically be after the latest commit/commits

Regarding the question «why?», I see several use cases, but my main intent of this exercise was to try to find a way to make part of your work seem to have been done at a later time then it was done initially, for instance if you sketched some code in the morning and wanted to be able to push it as if it was done in the evening.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

OK, I guess I was thinking about reordered Commits can happen.

Still not sure why the morning Commits would be desired to be changed for the evening.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.