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

后端 未结 21 2417
慢半拍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 09:03

    Set the date of the last commit to the current date

    GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
    

    Set the date of the last commit to an arbitrary date

    GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"
    

    Set the date of an arbitrary commit to an arbitrary or current date

    Rebase to before said commit and stop for amendment:

    1. git rebase ^ -i
    2. Replace pick with e (edit) on the line with that commit (the first one)
    3. quit the editor (ESC followed by :wq in VIM)
    4. Either:
    • GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
    • GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"

    Source: https://codewithhugo.com/change-the-date-of-a-git-commit/

提交回复
热议问题