Remove committed files from git history (easier way)

Remove committed files from git history (easier way) main picture
DEC17

Storing passwords, API keys, access tokens and other sensitive data directly in git repository is considered a bad practice and decreases security of your production environment and any third-party integrations that you might have. Also you might included images, fonts, videos or other large files in previous commits that are just making your repository larger, but you don't need them anymore.

In this article you will learn how to remove all that information from git history.

WARNING: Before proceeding with following instructions - backup your repository somewhere. Modifying git history might have irrevocable consequences. Proceed at your own risk.

1). Make sure nobody on your team works on any new feature or a bug fix. If they do - merge their code first, since after following steps they will not be able to do so, because git history will be altered.

2). Add all files that you need to remove from repository to .gitignore Probably you've tried that already, but git keeps track of changes in those files anyway.

3). Open terminal in your project folder and run

git rm --cached -r .

It should remove all the files from git repository.

4). Stage all files for commit

git add -A

5). Commit changes

git commit -m 'Removed unnecessary assets and secrets'

6). Push changes to remote repository

git push origin master -f

7). All your team member will have to pull changes with force flag

git pull origin master -f

Hope this article helps and your git repository will lose some weight and projects will become more secure.

Thanks for reading! See you soon 👍