views:

32

answers:

2

Hi all,

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?

+4  A: 

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.

Ether
doh! Thanks Ether, that should do it.
46and2
+2  A: 

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.

Chris Johnsen
Reordering strikes me as likely to fail frequently, as commits to be squashed often touch the same content, and putting the last one first is likely to result in conflicts. This seems like a much more useful answer to me! (+1)
Jefromi