Updating commit information can be essential for maintaining accurate project history or correcting errors. Whether you need to change a commit date for clarity, compliance, or other reasons, you have a couple of methods at your disposal.
This rule outlines how to change the date of an existing commit using both a manual CLI approach and an automated script.
git checkout -b {{ BRANCH NAME }} origin/{{ BRANCH NAME }}
git log
git rebase -i {{ COMMIT HASH }}^
GIT_COMMITTER_DATE="{{ NEW DATE IN 'YYYY-MM-DD HH:MM:SS' FORMAT }}" GIT_AUTHOR_DATE="{{ NEW DATE IN 'YYYY-MM-DD HH:MM:SS' FORMAT }}" git commit --amend --no-edit
git rebase --continue
git push origin {{ BRANCH NAME }} --force
If the date change is to be applied on several branches, it is preferable to automate the process with a script.
BRANCH=$1DATE=$2if [ -z "$BRANCH" ] || [ -z "$DATE" ]; thenecho "Usage: $0 {{ BRANCH NAME }} {{ NEW DATE IN 'YYYY-MM-DD HH:MM:SS' FORMAT }}"exit 1figit checkout -b "$BRANCH" "origin/$BRANCH"LAST_COMMIT_HASH=$(git log -n 1 --pretty=format:"%H")git rebase -i "$LAST_COMMIT_HASH^"GIT_COMMITTER_DATE="$DATE" GIT_AUTHOR_DATE="$DATE" git commit --amend --no-editgit rebase --continuegit push origin "$BRANCH" --forcegit checkout main
The script can be actioned with the following command:
./change_history.sh "{{ LOCAL PATH }}" "{{ NEW DATE IN 'YYYY-MM-DD HH:MM:SS' FORMAT }}"