views:

1231

answers:

2

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. These commits were not pushed anywhere yet, so I want to fix the time.

+3  A: 

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.

Alex Lebedev
A simple and clean way. Thanks!
goodrone
You can just use "hg strip" to remove the offending revision(s) instead of cloning.
skypher
+4  A: 

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
Ry4an
A good native way (though requires understanding of MQ). Thanks!
goodrone