Remove Files from Git Commit History Completely

Always, We need to remove sensitive or earlier pushed files form Git Hub public repository at some point of time, this is a quick guide how to remove files form Git hub commit history completely.

Before removing file, I would suggest backup your data, In case if anything goes wrong.

Say If you want to remove file config/oauth.yml form Git history, follow these steps.

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch config/oauth.yml" HEAD

This command will run the entire history of every branch and tag, changing any commit that involved the file config/oauth.yml, and any commits afterwards.

rm -rf .git/refs/original/ && git reflog expire --all &&  git gc --aggressive --prune

While git filter-branch rewrites the history for you, the objects remain in your local repository until they’ve been dereferenced and garbage collected. If you are working in your main repository, you might want to force these objects to be purged.

git push --force --verbose --dry-run
git push origin branch_name --force

If you need to force-push the changes to overwrite the remote repository.

Keep in mind that once you’ve pushed this code to a remote repository like GitHub and others have cloned that remote repository, you’re now in a situation where you’re rewriting history. When others try pull down your latest changes after this, they’ll get a message indicating that the the changes can’t be applied because it’s not a fast-forward.

To fix this, they’ll have to either delete their existing repository and re-clone it, or follow the instructions under “RECOVERING FROM UPSTREAM REBASE” in the git-rebase manpage.

References: GitHubStackoverflow.



Add your thoughts...

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.