Skip to main content

GitIgnore was not added to repo - apply in retrospect

Symptoms​

When working with a source-controlled project there are files which should not be tracked by Git (e.g. build folders). It's hard to work ith source control and commits will be complex (not just one altered code file).

Cause​

If a gitIgnore file is added to a repo after files haave already been tracked then it can be difficult to add in retrospect.

Resolution​

Source: [https://stackoverflow.com/questions/19663093/apply-gitignore-on-an-existing-repository-already-tracking-large-number-of-file](https://stackoverflow.com/questions/19663093/apply-gitignore-on-an-existing-repository-already-tracking-large-number-of-file)

First of all, `commit all pending changes.

Then:# Remove everything from the git index git rm -r --cached .

Create a .gitignore file

See https://github.com/github/gitignore for examples

git add .gitignore

Add a new commit

git add . git commit -m ".gitignore is now working"

Reflect the changes to the remote repo

git push origin