# Create an empty repositorymkdir gitignore-testcd gitignore-testgit init
# Create a file and commit itecho "hello" > filegit add filegit commit -m initial
# Add the file to gitignore and commitecho "file" > .gitignoregit add .gitignoregit commit -m gitignore
# Remove the file and commitgit rm filegit commit -m "removed file"
# Reintroduce the file and check status.# .gitignore is now respected - status reports "nothing to commit".echo "hello" > filegit status
#Commit up-to-date .gitignore (if not already existing)#This command must be run on each branch
git add .gitignoregit commit -m "Create .gitignore"
#Apply standard Git ignore behavior only to the current index, not the working directory (--cached)#If this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist#This command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
#Commit to prevent working directory data loss!#This commit will be automatically deleted by the --prune-empty flag in the following command#This command must be run on each branch
git commit -m "ignored index"
#Apply standard git ignore behavior RETROACTIVELY to all commits from all branches (--all)#This step WILL delete ignored files from working directory UNLESS they have been dereferenced from the index by the commit above#This step will also delete any "empty" commits. If deliberate "empty" commits should be kept, remove --prune-empty and instead run git reset HEAD^ immediately after this command
git filter-branch --tree-filter 'git ls-files -z --ignored --exclude-standard | xargs -0 git rm -f --ignore-unmatch' --prune-empty --tag-name-filter cat -- --all
#List all still-existing files that are now ignored properly#If this command returns nothing, it's time to restore from backup and start over#This command must be run on each branch
git ls-files --other --ignored --exclude-standard
#fetch modified remote
git fetch --all
#"Pull" changes WITHOUT deleting newly-ignored files from working directory#This will overwrite local tracked files with remote - ensure any local modifications are backed-up/stashed
git reset FETCH_HEAD