If I use
git rebase --interactive
and set one line to "edit", I can say
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?
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With