20

How can I change the time I've made a commit in my local repository?

Suppose I've done several commits and noticed that the date on my computer is wrong. Suppose also that these commits were not pushed anywhere yet.

4 Answers 4

16

If it's just a single commit, and that commit is the most recent one (on whatever branch you're on), a quick one-liner for doing this is:

hg commit --amend -d now
11

You can do it easily with MQ (Mercurial Queues):

Set up a bad date repo

+ hg init
+ echo line
+ hg commit -A -d 12/1 -m first
adding file
+ echo line
+ hg commit -A -d 12/2 -m second
+ echo line
+ hg commit -A -d 12/3 -m third
+ hg log
changeset:   2:81c88de729a8
tag:         tip
user:        Ry4an Brase <ry4an@mini>
date:        Thu Dec 03 00:00:00 2009 -0600
summary:     third

changeset:   1:c1fe70008824
user:        Ry4an Brase <ry4an@mini>
date:        Wed Dec 02 00:00:00 2009 -0600
summary:     second

changeset:   0:abb97adaa541
user:        Ry4an Brase <ry4an@mini>
date:        Tue Dec 01 00:00:00 2009 -0600
summary:     first

Turn the changesets into patches in the queue

+ hg qimport -r 2
+ hg qimport -r 1
+ hg qimport -r 0

Make each patch the qtip in turn and fix the date

+ hg qrefresh -D
+ hg qpop
Now at: 1.diff
+ hg qrefresh -D
+ hg qpop
Now at: 0.diff
+ hg qrefresh -D

Reapply the patches

+ hg qpush
applying 1.diff
Now at: 1.diff
+ hg qpush
applying 2.diff
Now at: 2.diff

Turn each patch back into real changesets

+ hg qdel -r 0
+ hg qdel -r 1
+ hg qdel -r 2

All better:

+ hg log
changeset:   2:6b51e14aadfc
tag:         tip
user:        Ry4an Brase <ry4an@mini>
date:        Wed Feb 25 22:29:01 2009 -0600
summary:     third

changeset:   1:5cbb9fc51bcc
user:        Ry4an Brase <ry4an@mini>
date:        Wed Feb 25 22:29:02 2009 -0600
summary:     second

changeset:   0:ec58d1f24278
user:        Ry4an Brase <ry4an@mini>
date:        Wed Feb 25 22:29:02 2009 -0600
summary:     first
4
  • 1
    A good native way (though requires understanding of MQ). Thanks!
    – Andrew T
    Feb 26, 2009 at 12:11
  • 1
    This inverts the time order cset 2 seems to have been committed before cset 1. I suggest the following procedure: import; pop all; push, refresh, push, refresh, push, refresh, delete all. That seems to do the trick while retaining progressive timestamps. Jan 20, 2011 at 13:00
  • You can set the dates on each to whatever you want with qrefresh, so really either method works the same. Jan 20, 2011 at 15:50
  • @AlanFranzoni: Use -d instead of -D and specify the date manually if you want the order to match the date order.
    – WhyNotHugo
    Feb 27, 2015 at 3:25
10

There's --date flag for hg commit, this is how you overwrite commit time. The question is how to recommit earlier changes without tool much pain.

Let's assume you get the following history of local commits:

dir1> hg commit # r100, OK
dir1> hg commit # r101, need to fix time
dir1> hg commit # r102, need to fix time

Here's my solution:

hg diff -r100:101 > 101.diff
hg diff -r101:102 > 102.diff
cd ..
hg clone -r100 dir1 dir2 # create a copy just before changesets than needs to be fixed
cd dir2
patch -i ../dir1/101.diff
hg commit -m "Same commit message" --date="required date"
patch -i ../dir1/102.diff
hg commit -m "Same commit message" --date="required date"
cd ..
rm -rf dir1 &&  mv dir2 dir1 # replace working copy

You can automate application of patches with hg patch which I did not use in my practice yet.

2
  • A simple and clean way. Thanks!
    – Andrew T
    Feb 26, 2009 at 12:09
  • 3
    You can just use "hg strip" to remove the offending revision(s) instead of cloning. Mar 1, 2009 at 13:25
2

Using hg's graft and strip seems like a simpler alternative to using MQ/patches/evolve.

With this method, you graft your commits onto a 2nd duplicate branch (while making use of the date changing functionality of graft). And then from there you can just strip back the branch with the bad dates.. Say for example, you've accidentally created some commits with bad dates and your history looks like the graph below:

> hg log -GT'{rev}:{desc} ({date|isodatesec})'
@  8:good commit (2018-03-18 20:13:07 2018 -0500)
|
o  7:erroneous commit two (2018-12-01 00:00:00 2018 -0600)
|
o  6:erroneous commit one (2018-12-01 00:00:00 2018 -0600)
|
o  5:commit before you started commiting bad dates

To fix this up, just update to the last good revision before your erroneous commits and then copy the commits over to a new (anonymous) branch using graft:

> hg up 5
0 files updated, 0 files merged, 1 files removed, 0 files unresolved
> hg graft -D -r6 -r7 -r8
> hg log -GT'{rev}:{desc} ({date|isodatesec})'
@  11:good commit (2018-03-18 20:14:48 2018 -0500)
|
o  10:erroneous commit two (2018-03-18 20:14:48 2018 -0500)
|
o  9:erroneous commit one (2018-03-18 20:14:48 2018 -0500)
|
| o  8:good commit (2018-03-18 20:13:07 2018 -0500)
| |
| o  7:erroneous commit two (2018-12-01 00:00:00 2018 -0600)
| |
| o  6:erroneous commit one (2018-12-01 00:00:00 2018 -0600)
|/
o  5:commit before you started commiting bad dates

Now you have two identical branches with the same commits buts different dates. So now you just need to return your linear history by using strip on the old branch:

> hg strip -r6 -r7 -r8
saved backup bundle to /home/miles/repo/.hg/strip-backup/ac1973513844-a8f5244e-backup.hg
> hg log -GT'{rev}:{desc} ({date|isodatesec})'
@  8:good commit (2018-03-18 20:14:48 -0500)
|
o  7:erroneous commit two (2018-03-18 20:14:48 -0500)
|
o  6:erroneous commit one (2018-03-18 20:14:48 -0500)
|
o  5:commit before you started commiting bad dates

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.