Skip to content

Instantly share code, notes, and snippets.

@sakkke
Last active October 7, 2022 05:26
Show Gist options
  • Save sakkke/4ecd07790205c3622312560e3de1bc5f to your computer and use it in GitHub Desktop.
Save sakkke/4ecd07790205c3622312560e3de1bc5f to your computer and use it in GitHub Desktop.
A function to edit author date and committer date of a git commit
# Usage: git-editdate 10 minutes ago
# Usage: git-editdate 3 hours 2 minutes 1 second
# Usage: git-editdate author
#
# Extra Usage:
# # Set command to edit since HEA0~10
# env GIT_SEQUENCE_EDITOR='sed --in-place "s/^pick /edit /"' git rebase --interactive HEAD~10
#
# # do-while
# while
# git-editdate author
# git rebase --continue
# do true; done
#
function git-editdate {
local date="$*"
local format='+%Y/%m/%d %H:%M:%S %z'
local old_author_date="$(old_author_date:-"$(git show --format=%ai --no-patch HEAD)")"
local old_author_email="$(old_author_email:-"$(git show --format=%ae --no-patch HEAD)")"
local old_author_name="$(old_author_name:-"$(git show --format=%an --no-patch HEAD)")"
local old_committer_date="$(old_committer_date:-"$(git show --format=%ci --no-patch HEAD)")"
local old_committer_email="$(old_committer_email:-"$(git show --format=%ce --no-patch HEAD)")"
local old_committer_name="$(old_committer_name:-"$(git show --format=%cn --no-patch HEAD)")"
local new_author_date
local new_author_email
local new_author_name
local new_committer_date
local new_committer_email
local new_committer_name
if [[ $date = author ]]; then
new_author_date="$old_author_date"
new_author_email="$old_author_email"
new_author_name="$old_author_name"
new_committer_date="$old_author_date"
new_committer_email="$old_author_email"
new_committer_name="$old_author_name"
elif [[ $date = committer ]]; then
new_author_date="$old_committer_date"
new_author_email="$old_committer_email"
new_author_name="$old_committer_name"
new_committer_date="$old_committer_date"
new_committer_email="$old_committer_email"
new_committer_name="$old_committer_name"
else
new_author_date="$(date --date="$old_author_date $date" "$format")"
new_author_email="$old_author_email"
new_author_name="$old_author_name"
new_committer_date="$(date --date="$old_committer_date $date" "$format")"
new_committer_email="$old_committer_email"
new_committer_name="$old_committer_name"
fi
env \
GIT_AUTHOR_DATE="$new_author_date" \
GIT_AUTHOR_EMAIL="$new_author_email" \
GIT_AUTHOR_NAME="$new_author_name" \
GIT_COMMITTER_DATE="$new_committer_date" \
GIT_COMMITTER_EMAIL="$new_committer_email" \
GIT_COMMITTER_NAME="$new_committer_name" \
git commit --allow-empty --allow-empty-message --amend --no-edit --reset-author
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment