remove file from git history code example

Example 1: git remove file from history

$ git rm --cached giant_file
# Stage our giant file for removal, but leave it on disk

Example 2: remove file history from git

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD

Example 3: github remove a file from a commit

## Remove Files From Git Commit
## In order to remove some files from a Git commit, use the “git reset”
## command with the “–soft” option and specify the commit before HEAD.
git reset --soft HEAD~1

## Now that your files are in the staging area, you can remove them (or
## unstage them) using the “git reset” command again.
git reset HEAD <file>
git rm --cached <file>

## When you are done with the modifications, you can simply commit your
## changes again with the “–amend” option.
git commit --amend

## Check file is no longer in the list
git ls-files

## Then if you're happy, you can do your push...

Example 4: remove a file from git commit

git reset --soft HEAD^ 
or
git reset --soft HEAD~1
git reset HEAD path/to/unwanted_file
git commit -c ORIG_HEAD

Example 5: how to remove git history for a file

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD

Example 6: how to remove one file from git history

To replace all text listed in passwords.txt wherever it can be found in your repository's history, run:
bfg --delete-files YOUR-FILE-WITH-SENSITIVE-DATA