Debdeep Bhattacharya

View My GitHub Profile

Common git scenarios

15 Jan 2022

Removing a large file from the commit history

If you want to remove a large file from the repository which was accidentally committed and pushed to GitHub (possibly due to a sloppy .gitignore), do

git filter-branch --force --index-filter \
 'git rm --cached --ignore-unmatch path/to/file.jpg' \
 --prune-empty --tag-name-filter cat -- --all

Then, to delete the same in the remote repo,

git push origin --force --all

Removing sensitive information from github repo

Main article

To change the email address of all previous commits

git filter-branch -f --commit-filter '
     if [ "$GIT_AUTHOR_EMAIL" = "OLD_EMAIL" ];
     then
             GIT_AUTHOR_EMAIL="NEW_EMAIL";
             git commit-tree "$@";
     else
             git commit-tree "$@";
     fi' HEAD

Then, force push the repo with

git push --force