Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git squash and preserve last commit's timestamp

Tags:

git

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.

like image 485
Crend King Avatar asked Apr 04 '12 23:04

Crend King


People also ask

Does git squash rewrite history?

Squash commits for a clean historyCommits marked with pick will have a new ID if the previous commits have been rewritten. Modern Git hosting solutions like Bitbucket now offer "auto squashing" features upon merge.

How do you squash the last n commits into a single commit?

You can use git merge --squash for this, which is slightly more elegant than git rebase -i . Suppose you're on master and you want to squash the last 12 commits into one. The documentation for git merge describes the --squash option in more detail.

How do I squash history in git?

The easiest one is to take advantage of Git repository servers like GitHub that typically have this built in within the pull/merge request feature. Usually you just need to tick a box saying you want to squash or to choose squash merge strategy and you're good to go. When it's merged, your branch gets squashed.


Video Answer


1 Answers

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.

like image 52
Richard Hansen Avatar answered Oct 03 '22 20:10

Richard Hansen