How can one change the timestamp of an old commit in Git?

后端 未结 21 2703
慢半拍i
慢半拍i 2020-11-22 08:36

The answers to How to modify existing, unpushed commits? describe a way to amend previous commit messages that haven\'t yet been pushed upstream. The new messages inherit t

21条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 08:49

    Use git filter-branch with an env filter that sets GIT_AUTHOR_DATE and GIT_COMMITTER_DATE for the specific hash of the commit you're looking to fix.

    This will invalidate that and all future hashes.

    Example:

    If you wanted to change the dates of commit 119f9ecf58069b265ab22f1f97d2b648faf932e0, you could do so with something like this:

    git filter-branch --env-filter \
        'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
         then
             export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
             export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
         fi'
    

提交回复
热议问题