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

  • checkout the first commit in the history containing a given String
  • git - How to checkout all changed files between two commits
  • How do you merge two Git repositories when one is a subdirectory of the other without losing commit history?
  • How to have the result of «git commit -v» in a splited window instead of in comments ?
  • Is there an upper limit to the number of commits a git repository can handle?
  • How to git commit a single file/directory
  • 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?

  • Access the changed files path in git pre-receive hook
  • Git does not detect repository
  • gitignore all files in all subdirectories except xml files
  • How does a Git repository works with a develop and master branch?
  • Simpler way to stage only modified files in Git (not deleted)
  • git-http-backend with AuthzUnixGroup not working properly
  • 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.