Tips & Tricks

Change the Date of a Commit (Author and Committer)

Sometimes you want to change the date of a commit, for example to correct a mistake or to mimic a specific timeline. To fully change a commit's date (so both the author and committer dates are updated and GitHub shows the new date), follow these steps:

Change the Date of the Last Commit

circle-check

Replace YYYY-MM-DD HH:MM:SS with your desired date and time.

Bash (Linux/macOS/Git Bash):

DATE="YYYY-MM-DD HH:MM:SS"
GIT_AUTHOR_DATE="$DATE" GIT_COMMITTER_DATE="$DATE" git commit --amend --date="$DATE" --no-edit

PowerShell (Windows):

$DATE="YYYY-MM-DD HH:MM:SS"
$env:GIT_AUTHOR_DATE=$DATE
$env:GIT_COMMITTER_DATE=$DATE
git commit --amend --date="$DATE" --no-edit

Change the Date of older commits

  1. Start an interactive rebase for the last X commits:

    git rebase -i HEAD~X
  2. In the editor that opens, change pick to edit for the commit you want to change, save and close the editor.

  3. Continue the rebase (done automatically if using VSCode Extension above):

     git rebase --continue
  4. Repeat 3. and 4. for the rest of the commits

  5. Push commit to your branch with the following command (replace branch-name with your target branch):

    git push --force-with-lease origin branch-name

Push commits in multiple times

The following command will not push X last commit(s):

Renaming branches

you can list your remotes with git remote -v but most of the time it's origin

Deleting branch

or

Set upstream

It tells Git: “When I push this local branch, also record that it should track that remote branch.”

Git will set your local testing branch’s upstream to origin/testing

This is the same as:

Last updated