Reputation: 715
How do I edit a commit's timestamp after (accidentally) pushing it to the remote repository?
I changed my system time clock to test behavior in my app, and forgot to set it back before
git add -u .
and git push
. Now my GitHub repo shows multiple files that were edited "in a month".
I have followed the advice here – specifically the comments to the OP that recommend git push --force
, but unfortunately the edit dates stay the same.
Upvotes: 14
Views: 19372
Reputation: 1322915
You could try the --date
option of git commit
, as described in this blog post by Alan Kelder:
The above works great for uncommitted changes, but what if you committed the changes and would like to modify the
AuthorDate
time? Again, git makes that easy for the last commit:
$ git commit --amend --date='<see documentation for formats>' -C HEAD
But what if you need to go back several commits? I ran into this situation as well and used the following Bash script to reset the
AuthorDate
time, here's what it does.For a list of files, it will get the hash of the last commit for each file, get the last modification date of the file, then reset the author timestamp to the file's actual modification date. Be sure to run it from the toplevel of the working tree (git will complain if you don't).
files="
foo.txt
bar.txt
"
for file in $files; do
dir=""; commit=""; date="";
dir=$(dirname $(find -name $file))
commit=$(git --no-pager log -1 --pretty=%H -- path "$dir/$file")
date="$(stat -c %y $dir/$file)"
if [ -z "$commit" ] || [ -z "$date" ]; then
echo "ERROR: required var \$commit or \$date is empty"
else
echo "INFO: resetting authorship date of commit '$commit' for file '$dir/$file' to '$date'"
git filter-branch --env-filter \
"if test \$GIT_COMMIT = '$commit'; then
export GIT_AUTHOR_DATE
GIT_AUTHOR_DATE='$date'
fi" &&
rm -fr "$(git rev-parse --git-dir)/refs/original/"
fi
echo
done
You could use the above script to reset both the
AuthorDate
andCommitDate
(just addGIT_COMMITTER_DATE
to the code above using the example from the Reference below).
Though modifying theCommitDate
for shared repositories sounds messy as you could be confusing others.
Upvotes: 7
Reputation: 2841
Well, that article is about changing the commit message, and then force pushing the change. But here in this case, you first need to fix the commit timestamp, before doing the git push --force
.
Read this article to see how to fix the timestamp: How can one change the timestamp of an old commit in Git?
And then force the push.
(Btw, on github, it may take a few refreshes before you see the change. Also, I assume you know that you are breaking the history and if somebody else cloned the repo, you broke them, etc etc, right? good!)
Upvotes: 9