How do I preserve the timestamp if I split a commit via git rebase –interactive?

If I use

git rebase --interactive

and set one line to “edit”, I can say

  • How to Avoid Committing Tokens in GIT
  • git : how to create a merge with a known outcome
  • Which SCM/VCS cope well with moving text between files?
  • How to pre-hook the gitflow hotfix finish?
  • How do I edit past git commits to remove my password from the commit logs?
  • Mark files as changed without changing them
  • git reset HEAD^
    git add -p
    git commit -m "First part."
    git commit -a -m "Second part."
    

    In other words, I split a commit. Unfortunately, this creates current timestamps for the two new commits, which makes the history look funny. Is there a way that the new commits share the timestamp of the one that it split?

  • Test VM in GIT repository
  • Why 'git ls-files' is not shown on tab completion?
  • Can anyone explaine why “git status” shows files as modfied when running under a share on linux?
  • 'git clone' with msysGit on Windows not working
  • msysgit32 insists on launching from HOMEDRIVE/HOMEPATH
  • Git merge – Access to: theirs; ours; merged-with-conflicts & nearest-ancestor
  • 2 Solutions collect form web for “How do I preserve the timestamp if I split a commit via git rebase –interactive?”

    If you’re using a recent version of Git, the SHA1 of the currently-being-edited commit during an interactive rebase appears to be stored in a file named .git/rebase-merge/amend.

    This information can be combined with git commit -c <commit>/--reedit-message=<commit> to enter the new message in an editor and preserve the original author timestamp.


    Putting it all together

    Do git rebase --interactive, mark that you want to edit a commit, then

    git reset HEAD^
    git add -p
    git commit -c $(cat .git/rebase-merge/amend)
    
    # 1. edit commit message to say "first part" (editor seeded with original message)
    # 2. save and quit
    
    git commit -a -c $(cat .git/rebase-merge/amend)
    
    # 1. edit commit message to say "second part" (editor seeded with original message)
    # 2. save and quit
    

    Note: If you’re trying to do this portably or in a script, you should use $(git rev-parse --git-dir) instead of .git.

    (Thanks @torek for that reminder.)

    It’s possible but not completely trivial. You just need to extract the timestamp out of the commit you’re splitting, and put that into the two new commits.

    Presumably you want to set the “author date” (maybe also the “committer date” as well, but maybe not). The method below is general enough to set both (use GIT_COMMITTER_DATE to set the other date).

    So, using the original method (separate git reset):

    $ export GIT_AUTHOR_DATE=$(git log -1 --pretty=format:%ad)
    

    You can now do your reset HEAD^ and commits; when you are done be sure to:

    $ unset GIT_AUTHOR_DATE
    

    so that this does not continue forcing the author-date on more commits.


    Alternatively, git commit --amend does a reset-and-commit for you, and preserves the author date. So only the second commit would have a different date, which you may override with either the environment variable, or the --date option:

    $ git rebase --interactive
    ...
    $ git commit --amend -m "First part."  # WITHOUT git reset HEAD^
    $ git commit --date="$(git log -1 --pretty=format:%ad)" -m "Second part."
    

    (These examples assume a shell that works like sh or bash.)

    Git Baby is a git and github fan, let's start git clone.