Skip to content

Instantly share code, notes, and snippets.

@m-kyle
Last active March 8, 2016 17:48
Show Gist options
  • Save m-kyle/fe9272a274b8e68c2c29 to your computer and use it in GitHub Desktop.
Save m-kyle/fe9272a274b8e68c2c29 to your computer and use it in GitHub Desktop.
Sample to to help fix Git repositories/commits

Fixing Git Commits

Replace/Update Users

#!/bin/sh
 
git filter-branch -f --env-filter '

OLD_EMAIL="david@example.com"
CORRECT_NAME="David"
CORRECT_EMAIL="davidd@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Fix Committer Name after git rebase

#!/bin/sh
 
git filter-branch -f --env-filter '

if [ "$GIT_COMMITTER_NAME" != "$GIT_AUTHOR_NAME" ]
then
    export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
fi
' --tag-name-filter cat -- --branches --tags

Replace blank lines

git filter-branch -f --msg-filter '
read msg
if [ -n "$msg" ] ; then
    echo "$msg"
else
    echo "--no commit message"
fi'

Start rebasing from 24th commit

git rebase -i HEAD~23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment