Skip to content

Removing File(s) From Git History

When a file needs to be removed from history (credential file maybe?) completely. Some steps need to be undertaken.

Modify the repository history

This command could take a long time if the repository is large.

Replace path_to_file to mach your file(s) using a wildcard or not.

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch path_to_file' \
  --prune-empty --tag-name-filter cat -- --all

Push all the commits

Since it seems the repository is diverted from the repository remote it must be forced to replace.

git push --all --force

When the remote is located on GitLab and is a Protected branch this may needs to be removed before it can be forced pushed.

Go to GitLab project "Settings" → "Repository" → "Expand" on "Protected branches".

Update other repository copies

To bring other repositories up-to-date execute the following commands:

git fetch origin master && git reset --hard FETCH_HEAD

Optionaly:

git clean -df