Skip to content

Instantly share code, notes, and snippets.

@HJunyuan
Last active June 25, 2021 16:51
Show Gist options
  • Save HJunyuan/2726c235776b653ed12ebaf5ba823bd2 to your computer and use it in GitHub Desktop.
Save HJunyuan/2726c235776b653ed12ebaf5ba823bd2 to your computer and use it in GitHub Desktop.
Modifying Git commit and author date

Modifying Git commit and author date

View Git log

# Pretty format
git log --pretty=fuller

# %ci: committer date, %ai: author date (e.g. "2021-06-09 01:18:55 +0800")
git log --graph --pretty=format:'%C(auto)%h%d (%ci) (%ai) <%ce> %s'

Source: https://stackoverflow.com/a/14244466/6805520

Use custom committer/author date for new commits

  1. Set env variables:

    export GIT_AUTHOR_DATE="YYYY-MM-DDThh:mm:ss±hh:mm"
    export GIT_COMMITTER_DATE="YYYY-MM-DDThh:mm:ss±hh:mm"
  2. Commit as usual:

    git add .
    git commit -m "Commit message"
  3. Unset env variables:

    unset GIT_AUTHOR_DATE
    unset GIT_COMMITTER_DATE

Source: https://garysferrao.github.io/git/commit/date/2015/12/01/change-git-commit-date.html

Modify committer date to use author date of past commits

Disclaimer: The following steps will modify your Git history.

  1. Start interactive rebase:

    # Interactive rebase
    git rebase -i HEAD~4 # Last 4 commits
  2. Add exec command below each commit:

    # Example
    pick 4ca564e Do something
    exec GIT_COMMITTER_DATE="$(exec git show -s --format=%ai)" git commit --amend --no-edit --date "$(exec git show -s --format=%ai)"
    pick 1670583 Add another thing
    exec GIT_COMMITTER_DATE="$(exec git show -s --format=%ai)" git commit --amend --no-edit --date "$(exec git show -s --format=%ai)"
    pick b54021c Add some tests
    exec GIT_COMMITTER_DATE="$(exec git show -s --format=%ai)" git commit --amend --no-edit --date "$(exec git show -s --format=%ai)"

    Source: https://stackoverflow.com/a/58622282/6805520

    Source: https://medium.com/hackernoon/change-the-date-of-a-git-commit-eeed8d2c5b9b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment