git squash and preserve last commit's timestamp

Go To StackoverFlow.com

20

Consider I have commits

... -- A -- B -- C

If I use git rebase -i to squash all three commits into one, we could

pick A
squash B
squash C

I see the resulted commit A has its original timestamp. How could make it inherit the timestamp of commit C (the last one)?

What I can think of is git commit --amend --date=<new_time>, but this way needs to remember the timestamp of commit C before squash or from reflog.

I find the timestamp of the latest commit is more reasonable, because it shows when I actually finished the work that are in the commits.

Thanks.

2012-04-04 23:24
by Crend King


11

There's not a trivial way to do this, but there are a few options. Here's one:

git commit --amend --date="$(git show -s --pretty=tformat:%ai <sha1-of-C>)"

And another:

git commit --amend -c <sha1-of-C>

The latter will clobber your existing commit message, so you'll have to rewrite it.

2012-04-05 00:35
by Richard Hansen
Why do you use tformat - Crend King 2012-04-05 01:03
@CrendKing: In this case it doesn't matter whether it's format or tformat because the tformat newline is stripped by the shell's command substitution rules. However, 99% of the time I want the final newline (or whatever the terminator is) because I'm usually parsing the custom log output with a script. In order to ensure that I habitually type tformat when it matters, I use tformat even when it doesn't matter - Richard Hansen 2012-04-05 04:38


5

git commit --amend --reset-author

2016-12-08 15:00
by nos
This doesn't use the timestamp from C, it uses the current time as the timestamp - Doug 2018-01-18 01:39
This does not do exactly what the OP requested, but (at least to my mind) closer matches what is usually wanted - David Goldfarb 2018-12-20 11:39
Ads