Git - Change the Date of Commits

Sometimes you might need to change the date of commits in your Git history. Whether it’s to correct a mistake, align commit dates with actual work dates, or simply backdate commits for demonstration purposes, Git provides tools to rewrite commit history. In this blog post, we’ll explore the steps to change commit dates in your Git repository.

Prerequisites

Before starting, ensure you have the following:

  1. Git installed on your system.
  2. A local copy of the Git repository where you want to change commit dates.

Important Considerations

Rewriting commit history can lead to significant changes, especially if the repository has been shared or pushed to a remote. When you modify commit dates, it changes the commit hashes, potentially causing conflicts for collaborators. It’s best to perform these changes on branches that have not been shared or to communicate with your team beforehand.

Changing the Date of the Most Recent Commit

If you need to change the date of the most recent commit, you can use the --amend option with git commit and set the new date using the GIT_COMMITTER_DATE and GIT_AUTHOR_DATE environment variables.

  1. Set the new date:

    export GIT_COMMITTER_DATE="Mon Apr 4 14:00 2022 +0100"
    export GIT_AUTHOR_DATE="Mon Apr 4 14:00 2022 +0100"
    
  2. Amend the most recent commit:

    git commit --amend --no-edit --date "$GIT_AUTHOR_DATE"
    
  3. Push the changes to the remote repository:

    git push --force
    

Changing the Date of Multiple Commits

To change the dates of multiple commits, you’ll need to use an interactive rebase. This allows you to edit each commit’s metadata, including the commit date.

  1. Start an interactive rebase:

    git rebase -i HEAD~n
    

    Replace n with the number of commits you want to rebase. For example, HEAD~5 will start an interactive rebase for the last five commits.

  2. Mark the commits for editing:

    In the text editor that opens, change pick to edit for each commit you want to change:

    edit <commit-hash> Commit message
    edit <commit-hash> Commit message
    
  3. Edit each commit date:

    For each commit marked for editing, Git will pause and allow you to change the commit date:

    export GIT_COMMITTER_DATE="Mon Apr 4 14:00 2022 +0100"
    export GIT_AUTHOR_DATE="Mon Apr 4 14:00 2022 +0100"
    git commit --amend --no-edit --date "$GIT_AUTHOR_DATE"
    git rebase --continue
    
  4. Repeat the process:

    Repeat the amend and continue steps for each commit you marked for editing.

  5. Push the rewritten history:

    git push --force
    

Changing the Date of All Commits

If you need to change the date of all commits in a repository, you can use a script to automate the process. Here’s an example using Git’s filter-branch command:

  1. Create a script:

    #!/bin/bash
    git filter-branch --env-filter '
    if [ "$GIT_COMMIT" = "<commit-hash>" ]
    then
        export GIT_AUTHOR_DATE="Mon Apr 4 14:00 2022 +0100"
        export GIT_COMMITTER_DATE="Mon Apr 4 14:00 2022 +0100"
    fi
    ' --tag-name-filter cat -- --branches --tags
    

    Replace <commit-hash> with the actual commit hash you want to change the date for. If you want to change the date for all commits, you can adjust the condition accordingly.

  2. Make the script executable:

    chmod +x change-date.sh
    
  3. Run the script:

    ./change-date.sh
    
  4. Push the changes to the remote repository:

    git push --force --tags origin 'refs/heads/*'
    

Conclusion

Changing the date of commits in Git history can be done using several methods, depending on the number and scope of commits involved. Whether you need to amend a single commit or rewrite the entire commit history, Git provides the tools necessary to make these changes. Always coordinate with your team and use these techniques carefully to avoid disrupting the shared repository history.

Published: Jan 13, 2022