Skip to content

Instantly share code, notes, and snippets.

@valera-rozuvan
Last active February 16, 2022 18:29
Show Gist options
  • Save valera-rozuvan/657ec5a55972d0f8061a1c485456d216 to your computer and use it in GitHub Desktop.
Save valera-rozuvan/657ec5a55972d0f8061a1c485456d216 to your computer and use it in GitHub Desktop.
howto amend commit with another date

howto amend commit with another date

So you want to add some changes to code, and make it as if your last commit has the changes. Also, you want to keep the date of your last commit. Easy!

  1. Do some changes.
  2. Make a temporary commit.
  3. Rebase interactive, squash last commit.
  4. Commit and amend. Set dates by exposing 2 ENV variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE.

So, it would look something like this, when doing via CLI:

git add .
git commit -m "update"
git rebase -i HEAD~2
GIT_AUTHOR_DATE="1641042000 +0600" GIT_COMMITTER_DATE="1641042000 +0600" git commit --amend
git push --force origin {BRANCH_NAME}

NOTE: Above, 1641042000 is a date represented by a unix timestamp, and +0600 is a time zone offset.

NOTE: Above, replace {BRANCH_NAME} with your active branch name (for example master).

soft reset approach

You can also do:

git reset --soft HEAD~1

This will get you back to the state you were in, before doing your last commit. You can do more changes, followed with:

GIT_AUTHOR_DATE="1641042000 +0600" GIT_COMMITTER_DATE="1641042000 +0600" git commit -m "some commit message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment