Cipher Log

Chronicles in Language, Technology, and Law

Backdate git commits with gittimetravelcommit

June 05, 2023

I previously wrote about backdating Git commits, i.e., making a Git commit have a different date and time from the current date and time. This is useful for migrating quick-and-dirty or old codebases filled with project_v1, project_v2, etc. folders into a structured Git repo with logs.

Recently, I was frustrated with this enough that I hacked together a bash/zsh function for my user profile. Add this to your .bash_profile or .zshrc to make it easy:

function gittimetravelcommit(){ if [ -e $1 ]; then GIT_COMMITTER_DATE="$(date -r "${1}" +'%Y-%m-%d %H:%M:%S %z')" GIT_AUTHOR_DATE="$(date -r "${1}" +'%Y-%m-%d %H:%M:%S %z')" git commit ${@:2} else echo "File '${1}' does not exist" echo "Usage: gittimetravelcommit <file to take timestamp from> [options for git commit]" fi }