Skip to content

Instantly share code, notes, and snippets.

@lisachacho
Last active May 6, 2025 22:52
Show Gist options
  • Save lisachacho/f23f5894c5db27c4eafbf9c22f36d788 to your computer and use it in GitHub Desktop.
Save lisachacho/f23f5894c5db27c4eafbf9c22f36d788 to your computer and use it in GitHub Desktop.
How to backdate pushed commits retroactively and how to set a different date for commits *right now* in Git and on Github πŸ•°οΈ πŸ”„

Custom-date a commit you haven't pushed yet

I haven't tested this with IDEs so I recommend using Git in the terminal, the old fashioned way. Do this all in the same terminal window!

Stage your changes:

git stage .

The . refers to the root folder, i.e. everything

Then, set your custom date by exporting two environment variables:

export GIT_AUTHOR_DATE="Fri May 2 08:23:39 2025 -0400" GIT_COMMITTER_DATE="Fri May 2 08:23:39 2025 -0400"

Now, commit your changes (with a commit message):

git commit -m "commit message"

Finally, clear your environment variables:

unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE

Remember to always unset the environment variables!!

Change pushed commits

Caution

1️⃣ Be sure you have the ability to force push to your repo/branch!

2️⃣ If you have any Github Actions, CI/CD, or any kind of checks attached to your repo, this will not trigger those checks.

3️⃣ Finally, if you do not unset the env variables which overwrite the commit date between each commit in the rebase (and after this is done), it will set every single commit to the same time. Be sure to unset!

Setup

First, take a look at your commits in order to decide which ones you want to modify:

git log

Grab the SHA from the commit before the earliest commit you'd like to change.

Rebase

Start the rebase

Let's start an interactive rebase (don't be scared):

git rebase -i <YOUR_SHA_HERE>

Running this command gives you a list of commits (in opposite order than you're used to) that looks something like this:

pick f7f3f6d initially creating the HTML file "Hello.html"
pick 310154e adding some code to "Hello.html"
pick a5f4a0d final cleanup

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Warning

Now you're in VIM.

Use the following command to edit the output:

i

And using your keyboard arrows, navigate to each of the commits which you'd like to edit.

Change each of those commits from pick to edit. For example:

pick f7f3f6d initially creating the HTML file Hello.html
pick 310154e adding some code to Hello.html
pick a5f4a0d final cleanup

# this will become...

edit f7f3f6d initially creating the HTML file Hello.html
edit 310154e adding some code to Hello.html
pick a5f4a0d final cleanup

Now, exit Insert mode in VIM by pressing ESCAPE on your keyboard.

Type the following and hit enter to finalize your changes:

:x

Git will begin the rebase and skip all the commits which you left as pick but stop on the commits that are labeled edit.

Edit the commit

Note

This process will be repeated with every commit for which you picked edit.

Git rewinds you back to the last edit commit in that list and drops you on the command line with the following message:

Stopped at f7f3f6d... initially creating the HTML file Hello.html
You can amend the commit now, with

       git commit --amend

Once you're satisfied with your changes, run

       git rebase --continue

Now that you're rewound to that commit, you can make changes!

We want to change two environment variables: GIT_AUTHOR_DATE and GIT_COMMITTER_DATE. These should have the same value.

export GIT_AUTHOR_DATE="Fri May 2 14:23:39 2025 -0400"
export GIT_COMMITTER_DATE="Fri May 2 14:23:39 2025 -0400"

Then, re-commit this change with the ammend flag:

git commit --amend

This will open VIM again. You can make changes to your commit message (i > Edit text > ESCAPE > :x) or just continue:

:x

Finally, unset the env variables and continue the rebase:

unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE
git rebase --continue

... and repeat.

Push

Ya gotta use force (don't be scared):

git push --force

Remember to always unset the environment variables!!

Cleanup

Remove the exported variables from our shell so as to not mess up any future committing:

unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE

Verify that this worked by checking all your environment variables:

printenv

πŸŽ‰

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