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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With