Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I preserve the time of a commit that is to be squashed into another?

I work in a branch 'develop', and when ready to merge with master, use interactive rebase to squash all my little commits into one feature-encompassing commit which I gets applied on top of master.

Works well, only issue I have is the time the commit is labelled is of the first small commit. It makes sense as that is the only commit that is 'picked', the rest are 'squashed'. Anyone know how to have the lumping commit be labeled with the time of the last small commit rather than the first? It may be possible to do so by 'editing' the earliest commit and squashing the later commits into this commit (which I presumably can edit the time property) however is there a better way?

like image 230
46and2 Avatar asked Sep 14 '25 08:09

46and2


2 Answers

If you can not just rearrange the commits to pick the one with your desired date (e.g. because the last commit will not apply cleanly atop the parent of the first commit), then you might try the following method.

If you have squashed together your develop commits into a single commit on develop, then you can use git show and the reflog of develop to extract the date of the original final commit (prior to squashing) and then amend the date of your new commit with git commit --date=.

git commit --amend -C HEAD --date="$(git show --pretty=format:%ad develop@{1})"

This amends (--amend) the HEAD commit so that

  • it has the commit message and date from HEAD,
    (-C HEAD; using this avoids starting an editor for the commit message just to immediately quit it without making any changes; leave this part off if you actually want to edit the commit message as well as changing the date)
  • its date is
    (--date=)
    • the same date as most recent prior tip of develop
      ("$(git show --pretty=format:%ad develop@{1})").

If develop@{1} does not have the date you want, then you can use git log -g develop to find a commit that has the date you want.

like image 188
Chris Johnsen Avatar answered Sep 15 '25 21:09

Chris Johnsen


Reorder the commits so the one that has the timestamp you want to preserve is the one that is picked, and then squash everything else into that one.

like image 36
Ether Avatar answered Sep 15 '25 22:09

Ether