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

后端 未结 21 2410
慢半拍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:56
    git commit --amend --date="now"
    
    0 讨论(0)
  • 2020-11-22 08:57

    A better way to handle all of these suggestions in one command is

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

    This will set the last commit's commit and author date to "right now."

    0 讨论(0)
  • 2020-11-22 08:58

    I wrote a script and Homebrew package for this. Super easy to install, you can find it on GitHub PotatoLabs/git-redate page.

    Syntax:

    git redate -c 3
    

    You just have to run git redate and you'll be able to edit all the dates in vim of the most recent 5 commits (there's also a -c option for how many commits you want to go back, it just defaults to 5). Let me know if you have any questions, comments, or suggestions!

    0 讨论(0)
  • 2020-11-22 08:59

    I created this npm package to change date of old commits.

    https://github.com/bitriddler/git-change-date

    Sample Usage:

    npm install -g git-change-date
    cd [your-directory]
    git-change-date
    

    You will be prompted to choose the commit you want to modify then to enter the new date.

    If you want to change a commit by specific hash run this git-change-date --hash=[hash]

    0 讨论(0)
  • 2020-11-22 08:59

    To change both the author date and the commit date:

    GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"
    
    0 讨论(0)
  • 2020-11-22 08:59

    If you want to perform the accepted answer (https://stackoverflow.com/a/454750/72809) in standard Windows command line, you need the following command:

    git filter-branch -f --env-filter "if [ $GIT_COMMIT = 578e6a450ff5318981367fe1f6f2390ce60ee045 ]; then export GIT_AUTHOR_DATE='2009-10-16T16:00+03:00'; export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; fi"
    

    Notes:

    • It may be possible to split the command over multiple lines (Windows supports line splitting with the carret symbol ^), but I didn't succeed.
    • You can write ISO dates, saving a lot of time finding the right day-of-week and general frustration over the order of elements.
    • If you want the Author and Committer date to be the same, you can just reference the previously set variable.

    Many thanks go to a blog post by Colin Svingen. Even though his code didn't work for me, it helped me find the correct solution.

    0 讨论(0)
提交回复
热议问题