Skip to content

Instantly share code, notes, and snippets.

@mckartha
Last active April 29, 2022 18:08
Show Gist options
  • Save mckartha/83f8eca68f67d678e00d4ee4ee696f53 to your computer and use it in GitHub Desktop.
Save mckartha/83f8eca68f67d678e00d4ee4ee696f53 to your computer and use it in GitHub Desktop.
These git bash utility scripts will execute git ops on a specific date, and change/update the Author & Commiter Name and Email for all commits in a workdir

Useful Git scripts

These git bash utility scripts will:

  • execute add and commit git operations on a specific date rather than the current time/date right now
  • change the Author & Commiter Name and Email for all commits in a workdir
  • update the Author & Commiter Name currently in use in gitconfig
# This script will take all commits and change the Author Name and Email and
# Commiter Name and Email to a new value
# This example came from:
# http://stackoverflow.com/questions/750172/how-do-i-change-the-author-of-a-commit-in-git
if [ $# == 0 ] ; then
echo "This script requires 2 parameters, and in double-quotes if they contain spaces, eg.:"
echo " * Author Name"
echo " * Author Email"
fi
Newname=$1
Newemail=$2
#git filter-branch -f --env-filter "GIT_AUTHOR_NAME='$Newname';
#GIT_AUTHOR_EMAIL='$Newemail'; GIT_COMMITTER_NAME='$Newname';
#GIT_COMMITTER_EMAIL='$Newemail';" HEAD
#With linebreaks in the string (which is possible in bash):
git filter-branch -f --env-filter "
GIT_AUTHOR_NAME='$Newname'
GIT_AUTHOR_EMAIL='$Newemail'
GIT_COMMITTER_NAME='$Newname'
GIT_COMMITTER_EMAIL='$Newemail'
" HEAD
#!/bin/bash
# This script executes a git operation using the file modification date
# (retrieved by the stat command) described by this wiki page:
# https://github.com/syntechdev/powerpanel/wiki/FunWithGitAndGithub#git-commits-using-file-mod-dates
# Unfortunately stat has different syntax on the Mac and on Linux (Grrr!) so to get a file date:
# On a Mac system execute:
# stat -f "%Sm" sql/parsed-payload-tracker.sql
# On a linux system execute:
# stat -c "%y" sql/parsed-payload-tracker.sql
# enable for debugging to echo commands as run
set -x
# extended pattern matching
shopt -s extglob
if [ $# -le 1 ] ; then
echo "This script requires at least 3 parameters, with double-quotes used if they contain spaces; and can take a 3rd param as the git add file list or the git commit message:"
echo " * Param 1: Git operation (add|mv|commit)"
echo " * Param 2: File to use to get date-time source"
echo " * Param 3: File(s) or directory to pass to the git command op (only one param works currently)"
echo " OR Git commit message (must be in quotes if it includes spaces)"
exit
fi
gitop=$1
statfile=$2
platform='unknown'
platform=`uname`
#echo "uname result: ** $platform **"
#if [[ "$platform" == "Darwin" ]]; then
case "$platform" in
"Darwin")
statcmd="stat -f %Sm $statfile"
;;
"Linux")
#elif [[ "$platform" == "Linux" ]]; then
statcmd="stat -c %y $statfile"
;;
#else
*)
# Unknown platform
echo "Uname returned an unexpected result so can't identify platform"
exit
;;
esac
#fi
#echo "$statcmd"
statdatetime=$($statcmd)
echo "File $statfile datetime reported by stat: $statdatetime"
#gitfiles=${3:-$statfile}
#gitmessage=${@:4}
#if [[ "$gitop" == ("add"|"mv") ]] ; then
case "$gitop" in
+("add"|"mv")) # gitop matches add or mv
# Add the files in the sql/ sub-directory
gitfiles=${@:3}
echo "*** $gitfiles ***"
#GIT_AUTHOR_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" GIT_COMMITTER_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" git $gitop sql/
# echo GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git $gitop $gitfiles
GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git $gitop $gitfiles
# Confirm that the correct files are staged for commit
git status
;;
#elif [[ "$gitop" == "commit" ]] ; then
"commit") # gitop matches commit
# Commit the files in the sql/ sub-directory
gitmessage=${@:3}
echo "*** $gitmessage ***"
# GIT_AUTHOR_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" GIT_COMMITTER_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" \
# git commit -m "Committing sql parsing files created in January 2014, prior to committing other changes"
# echo GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git commit -m $gitmessage
GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git commit -m "$gitmessage"
;;
#else
*)
echo "You entered [ $gitop ] as the Git operation but it was not recognized"
;;
esac
#fi
# This script will reset the git config user.name and user.email config
# varaibles so they are used for git commits as Author Name and Email and
# Commiter Name and Email
# This script is loosely based on ~/git-change-author.sh
if [ $# == 0 ] ; then
echo "This script requires at least 3 parameters, with use double-quotes if
they contain spaces; and can take a 4th param to control the git config scope eg.:"
echo " * Author Name"
echo " * Author-Email"
echo " * Github-Username"
echo " * Github-Scope (local|system|global)"
else
Newname=$1
Newemail=$2
Newgituser=$3
Gitscope=${4:-system}
echo "*** Git config parameters before update: ***"
# git config --get-regexp user.[ne]*
git config --get-regexp user*
echo "----------"
echo "*** Updating Git config parameters ... ***"
git config --${Gitscope} --replace-all user.name "$Newname"
git config --${Gitscope} --replace-all user.email $Newemail
git config --${Gitscope} --replace-all github.user $Newgituser
echo "----------"
echo "*** Git config parameters after update: ***"
# git config --get-regexp user.[ne]*
git config --get-regexp user*
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment