Skip to content

Instantly share code, notes, and snippets.

@pixelbrackets
Last active April 10, 2022 18:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbrackets/e2c2b451b77516e69377ecb4fd6f3a0d to your computer and use it in GitHub Desktop.
Save pixelbrackets/e2c2b451b77516e69377ecb4fd6f3a0d to your computer and use it in GitHub Desktop.
Change date of a selected commit or all commits
#!/usr/bin/env bash
# Usage:
# ./git-change-commit-date.sh <commit id | 'all' >
# Example:
# ./git-change-commit-date.sh e119b94 # change commit with id e119b94
# ./git-change-commit-date.sh all # changes all commits
#
# Warning! rebase/filter will change the history
COMMIT=$1
NEWDATE=$(date -R)
FILTER="true"
# check for missing argument
[ -z "$1" ] && echo "Missing arguments" && exit
# check for changes before rebasing
(git diff-files --quiet || (echo Unstaged changes, please commit or stash with --keep-index; exit 1))
echo "Change commit date of $COMMIT to $NEWDATE"
if [ "$COMMIT" != "all" ]
then
COMMIT=$(git rev-parse "$COMMIT")
FILTER="\$GIT_COMMIT = '$COMMIT'"
fi
git filter-branch -f --env-filter \
"if [ $FILTER ]
then
export GIT_AUTHOR_DATE='$NEWDATE'
export GIT_COMMITTER_DATE='$NEWDATE'
fi"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment