Skip to content

Instantly share code, notes, and snippets.

@toddsby
Last active July 16, 2019 07:09
Show Gist options
  • Save toddsby/55fda78bd089c28aeb8061f2bc12592c to your computer and use it in GitHub Desktop.
Save toddsby/55fda78bd089c28aeb8061f2bc12592c to your computer and use it in GitHub Desktop.
bash script git filter-branch change committer and author email
#!/bin/sh
echo "CHOOSE OPTION"
echo "[0] HEAD / [1] BRANCH NAME"
read REPLACE_EMAIL_OPTION
if [[ $REPLACE_EMAIL_OPTION = 0 ]]
then
GIT_BRANCH="HEAD"
else
echo "PROVIDE BRANCH NAME:"
read GIT_BRANCH
fi
echo "$REPLACE_EMAIL_OPTION"
echo "$GIT_BRANCH"
git filter-branch -f --env-filter '
# replace domain.com with your <domain>
GIT_EMAIL_PATTERN="^(.*)@.*\.domain\.com$"
if [[ $GIT_COMMITTER_EMAIL =~ $GIT_EMAIL_PATTERN ]] || [[ $GIT_AUTHOR_EMAIL =~ $GIT_EMAIL_PATTERN ]];
then
echo "WE HAVE A MATCH"
export GIT_COMMITTER_EMAIL="${BASH_REMATCH[1]}@domain.com"
export GIT_AUTHOR_EMAIL="${BASH_REMATCH[1]}@domain.com"
echo "$GIT_COMMITTER_EMAIL"
else
echo "NOPE"
fi
#echo "${BASH_REMATCH[0]}"
#echo "$GIT_COMMITTER_EMAIL"
' $GIT_BRANCH
@toddsby
Copy link
Author

toddsby commented Jul 16, 2019

if [[ $GIT_COMMITTER_EMAIL =~ $GIT_EMAIL_PATTERN ]]

if RegEx matches, then the capture groups are available in BASH_REMATCH[1], BASH_REMATCH[2], etc

read REPLACE_EMAIL_OPTION

read in a bash script waits for user input

$GIT_BRANCH git filter-branch requires the final param be the branch to apply the history rewrite, or HEAD for convenience

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment