536

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.

Pavan Nagadiya
  • 652
  • 4
  • 10
Tohid
  • 21,535
  • 8
  • 30
  • 43
  • 2
    you can `git rm --cached` entire directories with the `-r` option, if that's helpful – Nathan Wallace Oct 29 '13 at 15:52
  • 1
    See also: [Untrack Files From Git](http://stackoverflow.com/questions/6964297/untrack-files-from-git) – Patrick James McDougle May 29 '14 at 18:40
  • 3
    possible duplicate of [Ignore files that have already been committed to a Git repository](http://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository) – fkoessler May 28 '15 at 07:58
  • 2
    Possible duplicate of [Making git "forget" about a file that was tracked but is now in .gitignore](http://stackoverflow.com/questions/1274057/making-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – Patrick James McDougle Jul 30 '16 at 23:34

6 Answers6

1399

This answer solved my problem:

First of all, commit all pending changes.

Then run this command:

git rm -r --cached .

This removes everything from the index, then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"

Please be careful, when you push this to a repository and pull from somewhere else into a state where those files are still tracked, the files will be DELETED

john Smith
  • 17,409
  • 11
  • 76
  • 117
Tohid
  • 21,535
  • 8
  • 30
  • 43
  • If I done this and now i get an apache error of: "Warning: SuexecUserGroup directive requires SUEXEC wrapper." What shall I do? :-s – Ares Draguna Jul 25 '14 at 16:35
  • 28
    Perfect answer ! Please add a `git push origin` also to reflect the changes in the remote repository. – Sanket Berde Dec 01 '15 at 05:56
  • 17
    I started using this answer...and it deleted nearly everything in my project! Obviously I was doing something wrong. But I want to warn folks not to follow this suggestion casually. – Mark Olbert Jun 16 '16 at 16:18
  • 8
    Does this end up losing the history for the files when you remove and re-add them – Aran Mulholland Feb 28 '17 at 12:59
  • Is it possible to do that just for one subfolder/file of the project? – Abner Terribili May 16 '17 at 23:21
  • 68
    I wonder how many commits there are using this `.gitignore is now working` message. :) – ihavenoidea Jun 03 '19 at 20:23
  • Not working... the files which are not supposed to get track are now visible in git. There is a XML file with private credentials to the database which is now in git. – Black Aug 19 '19 at 07:41
  • 2
    This method worked well for me. I created a gitignore file, ignored an entire directory, then committed this change of creating the gitignore. I then followed these three commands above exactly as they said to. It worked perfectly, but I should say that you must "commit all pending changes" first as stated above. – Erich Meissner Apr 30 '20 at 18:39
  • 34
    @ihavenoidea https://github.com/search?q=.gitignore+is+now+working&type=Commits There you go! – bharadhwaj Jun 01 '20 at 09:04
  • 2
    June2020 25,144 commit results on github for `.gitignore is now working` – Abdur-Rahmaan Janhangeer Jun 25 '20 at 08:52
  • I did this on Windows for a project that is primarily (kinda) Linux-based. It ended up removing access permissions from all the files. In particular, it removed `x` from scripts. – MikeB Jan 27 '21 at 23:24
  • Please be careful, when you commit and push this to a repository and pull from somewhere else into a state where those files are still tracked it wil REMOVE them – john Smith Oct 23 '21 at 14:45
  • 1
    This is a dangerous answer, if you make a PR it tries to delete all the files. It does not answer the question that we want to git ignore them but not delete. – Karatekid430 May 20 '22 at 04:18
  • no file is being deleted, what do you mean? – Tohid Jul 18 '22 at 19:07
78
  1. Create a .gitignore file, so to do that, you just create any blank .txt file.

  2. Then you have to change its name writing the following line on the cmd (where git.txt is the name of the file you've just created):

rename git.txt .gitignore

  1. Then you can open the file and write all the untracked files you want to ignore for good. For example, mine looks like this:
    #OS junk files
    [Tt]humbs.db
    *.DS_Store
    
    #Visual Studio files
    *.[Oo]bj
    *.user
    *.aps
    *.pch
    *.vspscc
    *.vssscc
    *_i.c
    *_p.c
    *.ncb
    *.suo
    *.tlb
    *.tlh
    *.bak
    *.[Cc]ache
    *.ilk
    *.log
    *.lib
    *.sbr
    *.sdf
    *.pyc
    *.xml
    ipch/
    obj/
    [Bb]in
    [Dd]ebug*/
    [Rr]elease*/
    Ankh.NoLoad
    
    #Tooling
    _ReSharper*/
    *.resharper
    [Tt]est[Rr]esult*
    
    #Project files
    [Bb]uild/
    
    #Subversion files
    .svn
    
    # Office Temp Files
    ~$*

There's a whole collection of useful .gitignore files by GitHub

  1. Once you have this, you need to add it to your git repository just like any other file, only it has to be in the root of the repository.

  2. Then in your terminal you have to write the following line:

git config --global core.excludesfile ~/.gitignore_global

From oficial doc:

You can also create a global .gitignore file, which is a list of rules for ignoring files in every Git repository on your computer. For example, you might create the file at ~/.gitignore_global and add some rules to it.

Open Terminal. Run the following command in your terminal: git config --global core.excludesfile ~/.gitignore_global

If the respository already exists then you have to run these commands:

git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

If the step 2 doesn´t work then you should write the hole route of the files that you would like to add.

Anton
  • 2,669
  • 1
  • 7
  • 15
SomeAnonymousPerson
  • 3,173
  • 1
  • 21
  • 22
47

As specified here You can update the index:

git update-index --assume-unchanged /path/to/file

By doing this, the files will not show up in git status or git diff.

To begin tracking the files again you can run:

git update-index --no-assume-unchanged /path/to/file
Community
  • 1
  • 1
  • 8
    This is the better answer - it is focused only on the single file, the file that you don't want to be tracked by the repository. The highest marked answer will work as well, but it does much more than just ignore a file from tracking by git. – Krzysztof Chris Mejka Apr 07 '16 at 07:07
  • 1
    This is a good one. But what if we pull ? Are there any merge conflicts ? – pramod Apr 23 '16 at 06:21
  • @pramod, great question. I'm not really sure. Try it in a stripped down test repository? – Patrick James McDougle Jun 22 '16 at 23:25
45

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

Eli Nunez
  • 589
  • 5
  • 10
  • 2
    This is the best and clean answer if you want to remove only a few files, not the entire repository. I give you my thumbs up. – Apuig May 13 '19 at 10:50
  • Thank you!!! I've just level-ed up. Worked as described. After issuing `git commit` you can see the files affected, and then it removes them from the repo. Perfect! – Coffee and Code Feb 16 '21 at 22:26
  • For conceptual understanding only, assuming I have no other change, is the step "git add ." necessary in this case or is it a safe practice? – qqqqq Apr 24 '21 at 01:28
  • If there are no other staged changes, proceed to commit. – Eli Nunez Apr 26 '21 at 15:25
2

Here is one way to “untrack” any files that are would otherwise be ignored under the current set of exclude patterns:

(GIT_INDEX_FILE=some-non-existent-file \
git ls-files --exclude-standard --others --directory --ignored -z) |
xargs -0 git rm --cached -r --ignore-unmatch --

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).

  • sounds like a solid solution :) I presume that this is for Linux shell right ? unfortunately I am a Win7 user. Any way I can do this in windows ? – Tohid Oct 29 '13 at 16:01
-5

Use git clean
Get help on this running

git clean -h

If you want to see what would happen first, make sure to pass the -n switch for a dry run:

git clean -xn

To remove gitingnored garbage

git clean -xdf

Careful: You may be ignoring local config files like database.yml which would also be removed. Use at your own risk.

Then

git add .
git commit -m ".gitignore is now working"
git push
Nedudi
  • 5,639
  • 2
  • 42
  • 37