Git/History/Rewriting
From charlesreid1
via https://www.reddit.com/r/git/comments/2pjgsx/git_how_do_i_modify_timestamps_on_commit_objects/
How to modify entire commit history
Suppose you want to modify your entire git commit history. To do that, you can use the git filter-branch
command to run a command/script on every commit that can be reached from HEAD (i.e., starting from HEAD and traversing backwards to root).
git filter-branch --commit-filter CMD
CMD
is a command that is passed the same arguments as git commit-tree
.
It should do what git commit-tree
does.
(Which is...?)
Here's an example (executable) shell script called /tmp/rewrite.sh
:
#!/bin/sh export GIT_AUTHOR_DATE=1234567890+0000 export GIT_COMMITTER_DATE=1234567890+0000 git commit-tree "$@"
Now, when you rewrite the history, the entire history will appear to have been committed at the same timestamp, 1234567890:
Before:
$ git log commit 21a52d0c3a27faa1f8a0c3dc894ffb3f529b0d65 Author: Your Name Date: Tue Dec 16 23:50:51 2014 -0500 second commit commit 2e7a613b24c55ba383440a88dc9b5b55cddca5ad Author: Your Name Date: Tue Dec 16 23:50:38 2014 -0500 first commit
Apply git filter-branch:
$ git filter-branch --commit-filter '/tmp/rewrite.sh "$@"' Rewrite 21a52d0c3a27faa1f8a0c3dc894ffb3f529b0d65 (2/2) Ref 'refs/heads/master' was rewritten
After:
$ git log commit 15bf1d225ffb17103e0476687dcd6e0a5ce683c8 Author: Your Name Date: Fri Feb 13 23:31:30 2009 +0000 second commit commit b864f505b2a923b69f9da8dc7cb1c19d45dad92f Author: Your Name Date: Fri Feb 13 23:31:30 2009 +0000 first commit
Original refs are still stored in .git/refs/original
:
$ cat .git/refs/original/refs/heads/master 21a52d0c3a27faa1f8a0c3dc894ffb3f529b0d65
The filter command is passed the same arguments as git commit-tree.
The first argument will be the tree of the commit to rewrite.
You could write a more complex script which views the contents of that tree and sets the date appropriately.