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

后端 未结 21 2426
慢半拍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:50

    Just do git commit --amend --reset-author --no-edit. For older commits, you can do an interactive rebase and choose edit for the commit whose date you want to modify.

    git rebase -i <ref>
    

    Then amend the commit with --reset-author and --no-edit to change the author date to the current date:

    git commit --amend --reset-author --no-edit
    

    Finally continue with your interactive rebase:

    git rebase --continue
    
    0 讨论(0)
  • 2020-11-22 08:50

    How to Edit Multiple Commit Dates

    Other answers aren't very convenient for editing several commit dates. I've come back to this question after a few years to share a technique.

    To change the dates of the last 4 commits:

    git rebase -i HEAD~4
    

    Edit the rebase as follows, inserting exec lines to modify dates as needed:

    pick 4ca564e Do something
    exec git commit --amend --no-edit --date "1 Oct 2019 12:00:00 PDT"
    pick 1670583 Add another thing
    exec git commit --amend --no-edit --date "2 Oct 2019 12:00:00 PDT"
    pick b54021c Add some tests
    exec git commit --amend --no-edit --date "3 Oct 2019 12:00:00 PDT"
    pick e8f6653 Fix the broken thing
    exec git commit --amend --no-edit --date "4 Oct 2019 12:00:00 PDT"
    
    0 讨论(0)
  • 2020-11-22 08:51

    For those using Powershell

    git rebase DESIRED_REF^ -i
    
    $commitDateString = "2020-01-22T22:22:22"
    $env:GIT_COMMITTER_DATE = $commitDateString
    git commit --amend --date $commitDateString
    $env:GIT_COMMITTER_DATE = ""
    
    git rebase --continue
    

    Credit to https://mnaoumov.wordpress.com/2012/09/23/git-change-date-of-commit/

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

    The following bash function will change the time of any commit on the current branch.

    Be careful not to use if you already pushed the commit or if you use the commit in another branch.

    # rewrite_commit_date(commit, date_timestamp)
    #
    # !! Commit has to be on the current branch, and only on the current branch !!
    # 
    # Usage example:
    #
    # 1. Set commit 0c935403 date to now:
    #
    #   rewrite_commit_date 0c935403
    #
    # 2. Set commit 0c935403 date to 1402221655:
    #
    #   rewrite_commit_date 0c935403 1402221655
    #
    rewrite_commit_date () {
        local commit="$1" date_timestamp="$2"
        local date temp_branch="temp-rebasing-branch"
        local current_branch="$(git rev-parse --abbrev-ref HEAD)"
    
        if [[ -z "$date_timestamp" ]]; then
            date="$(date -R)"
        else
            date="$(date -R --date "@$date_timestamp")"
        fi
    
        git checkout -b "$temp_branch" "$commit"
        GIT_COMMITTER_DATE="$date" git commit --amend --date "$date"
        git checkout "$current_branch"
        git rebase "$commit" --onto "$temp_branch"
        git branch -d "$temp_branch"
    }
    
    0 讨论(0)
  • 2020-11-22 08:53

    If you want to get the exact date of another commit (say you rebase edited a commit and want it to have the date of the original pre-rebase version):

    git commit --amend --date="$(git show -s --format=%ai a383243)"
    

    This corrects the date of the HEAD commit to be exactly the date of commit a383243 (include more digits if there are ambiguities). It will also pop up an editor window so you can edit the commit message.

    That's for the author date which is what you care for usually - see other answers for the committer date.

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

    There are already many great answers, but when I want to change date for multiple commits in one day or in one month, I don't find a proper answer. So I create a new script for this with explaintion, hope it will help someone:

    #!/bin/bash
    
    # change GIT_AUTHOR_DATE for commit at Thu Sep 14 13:39:41 2017 +0800
    # you can change the data_match to change all commits at any date, one day or one month
    # you can also do the same for GIT_COMMITTER_DATE
    
    git filter-branch --force --env-filter '
    
    date_match="^Thu, 14 Sep 2017 13+"              
    
    # GIT_AUTHOR_DATE will be @1505367581 +0800, Git internal format 
    author_data=$GIT_AUTHOR_DATE;                   
    author_data=${author_data#@}                  
    author_data=${author_data% +0800}                # author_data is 1505367581     
    
    oneday=$((24*60*60))
    
    # author_data_str will be "Thu, 14 Sep 2017 13:39:41 +0800", RFC2822 format
    author_data_str=`date -R -d @$author_data`      
    
    if [[ $author_data_str =~ $date_match ]];
    then
        # remove one day from author_data
        new_data_sec=$(($author_data-$oneday))
        # change to git internal format based on new_data_sec
        new_data="@$new_data_sec +0800"             
        export GIT_AUTHOR_DATE="$new_data"
    fi
    ' --tag-name-filter cat -- --branches --tags
    

    The date will be changed:

    AuthorDate: Wed Sep 13 13:39:41 2017 +0800
    
    0 讨论(0)
提交回复
热议问题