Rewriting GIT history: how to remove undesirable folders or files
The history of a GIT repository is like a black hole: it absorbs and retains everything that crosses its event horizon. Not to mention, it can sometimes be a dump (just kidding). But seriously, sometimes things end up in the GIT repository that we didn’t intend to commit or might be embarrassed about in the future; for example, database credentials, unnecessary project files from our favorite IDE, or simply a shopping list for the evening. Unlike a black hole, however, there is a way to escape from the history of a GIT repository, which is reassuring.
Once, a former colleague of mine, who had moved to a new job, reached out for help because he had accidentally committed user passwords hardcoded in one of the files in his initial commits. He asked if I knew how to erase this unfortunate mistake from the history and pretend it never happened. In such a situation, I would recommend acknowledging that a leak occurred and that the credentials were compromised, deleting the files, and ensuring to disable them and create new credentials in the DBMS (including reconfiguring all systems that require them). But I also noted that, yes, there is a way to erase this file from the memory.
Attention: Before rewriting the GIT history in a work repository, please consult with your Team Lead or team, as these changes require a forced push of changes, which:
- might be impossible due to branch restriction settings in the repository;
- could cause conflicts for other people working with this repository.
Moreover, since something can always go wrong, it’s best to make a backup copy of the repository before starting to rewrite the GIT history.
To change the author data of commits in GIT, you need to:
1 — Make sure that the local repository contains all the changes from the remote; if necessary, execute the command:
git pull
2 — Execute the command to rewrite the history, specifying the command for deleting a file (rm file_name
) or a folder (rm -rf folder_name
). For instance, the command for clearing the .idea
folder from the history:
git filter-branch --tree-filter "rm -rf bin" --prune-empty --all
This command will remove the .idea
folder from each commit in the history and will prune empty commits that do not contain any changes after the folder removal.
Explanation of the command:
- git filter-branch: Starts the process of rewriting the commit history.
- — tree-filter “rm -rf .idea”: For each commit, executes the specified command to remove the
.idea
folder. - — prune-empty: Removes any commits that become empty after the specified files or folders are deleted.
- — all: Applies the filter to all branches and tags in the repository.
3 — Wait for the command to complete
4 — Force push the changes to the remote repository:
git push -f
If you follow these steps and everything goes correctly, then congratulations — the history will be rewritten.