Skip to content

How to modify date and time of old commits in Git

Sorry if this post is a bit abrupt / lacks details, it was really written as a quick memo for myself. You will find plenty more details in the sources listed at the bottom of the post (although most of them are simply what shows up first in DuckDuckGo when you search for how to edit old commits).

For this example, we will edit the last 10 commits:

git rebase --interactive HEAD~10

NB: if you’re using SourceTree, you can open a console to Git via the Menu: Actions => Open in Terminal

This will open a text editor with a listing of commits to process, like:

edit e1186e8 Oldest commit
edit 830e58c ANother commit
edit 9d41af7 Latest commit description

By default on Windows, for me the editor was that HORRIBLE shit called Vim. To start editing the text, press “Insert” key. Change the pick keyword to edit for the commits you want to modify. When you’re done, to save and quit press “Escape” (this leaves text editing mode), then type “:wq” and hit “Enter”.

Rebase will then process, telling you “You can amend the commit now”. To change the date without opening an editor (the editor would allow you to edit the commit text I think), use:

git commit --amend --date="20160101T12:12:12" --no-edit

(NB: for supported date formats, see here)

If you’re happy with your change, you can go on to the next commit with

git rebase --continue

Once you’ve run this command on the last commit, your branch will be updated (“Successfully rebased and updated refs/heads/master.”). If you screw up, git rebase --abort should revert all your changes (which seem to only get applied once you’re done with the last commit).

Last but not least, there seems to be a better way to edit the date, using something like this:

GIT_COMMITTER_DATE="20160101T12:12:12" git commit --amend

But I haven’t tried this yet.

A bunch of sources that helped (even though they all had big flaws that still made this a headache):

Squashing commits with rebase

PS: a little bonus: to “uncommit” the last commit (this cancels the commit but leaves the commit + the content of the commit is still selected and ready to be committed again): reset --soft HEAD^

PS2: if you try to run rebase while still having uncommitted changes, you’ll get an error saying “Cannot pull with rebase: You have unstaged changes. Please commit or stash them.” As the message says, you can commit your changes to bypass this, but if you don’t want to commit, you can run “git stash”, then do the rebase procedure, then at the end run “git stash pop”. Cf this stackoverflow post.

Posted in programming.

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Anno Nimouse says

    If the repo’s SSH key isn’t in your keyring and SourceTree isn’t able to automatically add it, run plink like this and say “yes” (or maybe just “y”)
    plink -T
    (of course replace bitbucket with whatever your git provider is)

Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA