I did not use .gitignore to ignore files such as compiled binaries in my project, so these compiled binaries have been committed into the Git repository. And now I add .gitignore to my project. I want to rebuild the whole Git repo ignoring the files listed in .gitignore without losing any history. How can I do that?
Remove those binary from the current cache if they were just added.
git rm --cached abinary
.gitignore will automatically ignores those binaries.
That will rewrite your history, and you will need a
git push --force, so if you have more than one (yourself) collaborator, warn them in advance.