I have an existing Visual Studio project in my repository. I recently added a .gitignore file under my project and I assume that tells Git to ignore the files listed in the file.
My problem is that all those files are already being tracked and as far as I know Git will not ignore a file that was already tracked before a rule was added to this file to ignore it.
It was suggested to use: git rm --cached
and manually un-track them but that's going to take me forever to go through them one by one.
I thought about deleting the repository and recreating it again but this time with .gitignore file present, but there must be a better way to do this.
Use
git clean
Get help on this running
If you want to see what would happen first, make sure to pass the -n switch for a dry run:
To remove gitingnored garbage
Careful: You may be ignoring local config files like database.yml which would also be removed. Use at your own risk.
Then
As specified here You can update the index:
By doing this, the files will not show up in
git status
orgit diff
.To begin tracking the files again you can run:
I think this is an easy way for adding a .gitignore file to an existing repository.
Prerequisite:
You need a browser to access your github account.
Steps
Have fun!
Here is one way to “untrack” any files that are would otherwise be ignored under the current set of exclude patterns:
This leaves the files in your working directory but removes them from the index.
The trick used here is to provide a non-existent index file to git ls-files so that it thinks there are no tracked files. The shell code above asks for all the files that would be ignored if the index were empty and then removes them from the actual index with git rm.
After the files have been “untracked”, use git status to verify that nothing important was removed (if so adjust your exclude patterns and use git reset -- path to restore the removed index entry). Then make a new commit that leaves out the “crud”.
The “crud” will still be in any old commits. You can use git filter-branch to produce clean versions of the old commits if you really need a clean history (n.b. using git filter-branch will “rewrite history”, so it should not be undertaken lightly if you have any collaborators that have pulled any of your historical commits after the “crud” was first introduced).
This answer solved my problem:
First of all, commit all pending changes.
Then run this command:
This removes everything from the index, then just run:
Commit it:
If you added your
.gitignore
too late, git will continue to track already commited files regardless. To fix this, you can always remove all cached instances of the unwanted files.First, to check what files are you actually tracking, you can run:
git ls-tree --name-only --full-tree -r HEAD
Let say that you found unwanted files in a directory like
cache/
so, it's safer to target that directory instead of all of your files.So instead of:
git rm -r --cached .
It's safer to target the unwanted file or directory:
git rm -r --cached cache/
Then proceed to add all changes,
git add .
and commit...
git commit -m ".gitignore is now working"
Reference: https://amyetheredge.com/code/13.html