How to ignore previously committed files in Git

Recently I started to work with the IntelliJ IDEA. As I pulled an old project I have seen that it contains Eclipse project files. My first task was to clean up my project and add the Eclipse and IntelliJ files to .gitignore. To get rid of the Eclipse files from the repository I used the following commands:

// Commit all changes (Maybe not necessary in your case)
git add . && git commit -m "Commit all staged files"

// Remove all files from the index
git rm -r --cached .

// Commit the files again without the ignored ones
git add . && git commit -m "Ignore project files"

Firstly I committed all recently done changes. Otherwise, I would lose them. The next command removes all index files recursively. The –cached option is used to remove the files from the index only and not from the working tree. The dot means that all files will be removed from the index. After that, I added all files again and did the commit. Now the repository is cleaned up, again.

If you want to test this command you can add the -n option. This will show you if the files exist and that they will be removed by using this command without this option.

// Shows the existing files in the index which would be removed by the command.
git rm -r -n --cached .

You can also remove specific files, only if you exchange the dot with the file name.

// Remove foo.log file from the index only
git rm -r --cached foo.log

If you want to learn more about the different options take a look into the Git documentation.

chevron_left
chevron_right

Leave a comment

Your email address will not be published. Required fields are marked *

Comment
Name
Email
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.