git delete ignored files code example

Example 1: git remove file from repo that has been added to gitignore

Commit any files that you need to change/delete to the repo. Then:

git rm -r --cached .  # this will clean all files from cache. To clean a single file, exchange '.' with filename
git add .  # add all of the now uncached files that are not gitignored to the staging area
git commit -m "fix .gitignore"  # commit the changes

Example 2: git remove untracked files

# Print out the list of files which will be removed (dry run)
git clean -n

# Interactive and you will get a quick overview of what is 
# going to be deleted offering you the possibility to include/exclude 
# the affected files
git clean -i

# To remove files, run
git clean -f

# To remove directories, run 
git clean -fd

# To remove ignored files, run 
git clean -fX

# To remove ignored and non-ignored files, run 
git clean -fx

Example 3: clear untracked files git

git clean -n