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

  • git filter-branch --msg-filter to reword a pushed commit message
  • Does git smartly handle a zip archive in which only one of the files changes regularly?
  • Change the wrong commit message in Git
  • Trouble with first commit to git repository
  • How to find commit SHA1 of a tree containing a file containing a given string
  • How to edit Git “add patch” hunks/diffs/lines during selective staging?
  • 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?

  • How to find out or change url for Git Repository server
  • Heroku NodeJS Help – Push rejected, no Cedar-supported app detected
  • howto use “No Fast Forward” flag when merging branches in git with IDEA
  • In what circumstances should I add the -a flag to the git tag command?
  • GIT author authentication
  • GitHub Desktop: “This file is empty”
  • 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.