zzh1996 zzh1996 -4 years ago 248
Git Question

Make Git rebuild history using the current .gitignore

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?

Answer Source

Remove those binary from the current cache if they were just added.

git rm --cached abinary

Then your .gitignore will automatically ignores those binaries.

But if they were committed in past commits, You won't loose history, but you still need to remove those binaries from said commit.
You will need either git filter-branch or bfg repo cleaner.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download