Andrew T
Andrew T

Reputation: 5745

How do I change commit time (not pushed yet)?

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.

Upvotes: 20

Views: 6627

Answers (4)

Miles Alan
Miles Alan

Reputation: 11

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

Upvotes: 1

Eric
Eric

Reputation: 5365

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

Upvotes: 16

Ry4an Brase
Ry4an Brase

Reputation: 78350

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

Upvotes: 10

Alexander Lebedev
Alexander Lebedev

Reputation: 6044

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.

Upvotes: 10

Related Questions