26

When I run git status:

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   .gitignore
#       modified:   .project
#       modified:   reports/images/2014-03.png
#       modified:   reports/images/graph-2014-03.bmp
#       deleted:    reports/phpbmp/cache/0/02/027/0270/phpThumb_cache_portal.gep.co.za_src02707010e1e50bc80594
f31460d514c4_par80d8993b181666aca11d7be02b12fea7_dat1399293161.bmp
#       deleted:    reports/phpbmp/cache/0/02/027/0270/phpThumb_cache_portal.gep.co.za_src02707010e1e50bc80594
f31460d514c4_par80d8993b181666aca11d7be02b12fea7_dat1399293182.bmp
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       README.MD
#       reports/phpbmp/cache/9/
no changes added to commit (use "git add" and/or "git commit -a")

I realised that I don't want changes to the following files be tracked:

.project
reports/images/*.png
reports/images/*.bmp
reports/phpbmp/cache/*

So I added these files to .gitignore

How do I remove the files from the current working directory under Changes not staged for commit?

tread
  • 10,133
  • 17
  • 95
  • 170

2 Answers2

78

If I understand well you want filename to be in the git repository. However you don't want to be notified of new changes to filename. You can do this using this command:

git update-index --assume-unchanged <filename>

If you wanna start tracking changes again run the following command:

git update-index --no-assume-unchanged <filename>

additional info: editing .gitignore will only ignore files so they will not be added to the git repository. However files that are tracked already will not be ignored.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • 1
    The problem is that if your changes can still cause conflicts and they'll just be harder to detect. You won't be able to stash or commit the changes either so you may end up stuck not being able to `git checkout` to other branches. – Jorge Orpinel Pérez Jan 13 '17 at 06:51
  • 1
    Yes. Important to note the above is considered bad practise by me. Better to remove these files from the repo, if you do not want to track changes. – tread Feb 21 '18 at 08:14
  • 1
    i did this to a file and now when i make changes to it and pull `Your local changes to the following files would be overwritten by merge: Please commit your changes or stash them before you merge. Aborting` – Abhi Apr 04 '19 at 07:30
  • 2
    This answer is correct. The "This question already has answers..." link is *not* correct, and *not* helpful. I hate it when editors don't read the actual question and all its nuances, and mark what is actually a unique case, as a duplicate. – Colin Feb 11 '23 at 23:27
  • One use for this: if you publish a web site via github pages, you do *not* want to overwrite the CNAME file. One good practice is to have a test site separate from your production site. Each site has a different CNAME file. Your Page Designer software will not know (and should not know) how to handle the CNAME file. Consider it as a "configuration" file specific to the site. Having Git ignore that file, so there is never a chance of overwriting it and taking your site down, is *very* useful. – Colin Feb 11 '23 at 23:31
-2

Files are already tracked and now you want to ignore those files.

git rm --cached filename 
echo "filename" >> .gitignore
git add .gitignore
git commit -m 'Removes filename file and adds it to gitignore.'
Vivart
  • 14,900
  • 6
  • 36
  • 74
  • This gives me: `# Changes to be committed: # (use "git reset HEAD ..." to unstage) # # deleted: .project`, doesn't this mean the file will be deleted or not exist at all when someone pulls from the repo? – tread May 15 '14 at 08:48
  • 1
    yes indeed; that will remove your file from the repository... I guess you need to use --assume-unchanged... – Chris Maes May 15 '14 at 09:15