Skip to content

Instantly share code, notes, and snippets.

@lukaszjablonski
Created July 18, 2018 10:48
Show Gist options
  • Save lukaszjablonski/63c3fb3b4876290b4123d43f00ee0c7c to your computer and use it in GitHub Desktop.
Save lukaszjablonski/63c3fb3b4876290b4123d43f00ee0c7c to your computer and use it in GitHub Desktop.
Change the date of git commit
#!/bin/bash
# Change the date of git commit
# souce: https://stackoverflow.com/a/24584976
# usage: git cdc commit date
# example: git cdc @~ "2014-07-04 20:32:45" would reset date of the commit before HEAD "@~" to "2014-07-04 20:32:45"
#
# commit
# date YYYY-mm-dd HH:MM:SS
commit="$1" datecal="$2"
temp_branch="temp-rebasing-branch"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
date_timestamp=$(date -d "$datecal" +%s)
date_r=$(date -R -d "$datecal")
if [[ -z "$commit" ]]; then
exit 0
fi
git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date_timestamp" GIT_AUTHOR_DATE="$date_timestamp" git commit --amend --no-edit --date "$date_r"
git checkout "$current_branch"
git rebase --autostash --committer-date-is-author-date "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment