Last active
November 21, 2024 12:07
-
-
Save pixelbrackets/e2c2b451b77516e69377ecb4fd6f3a0d to your computer and use it in GitHub Desktop.
Change date of a selected commit or all commits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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