594

I have committed loads of files that I now want to ignore. How can I tell git to now ignore these files from future commits?

EDIT: I do want to remove them from the repository too. They are files created after ever build or for user-specific tooling support.

3

11 Answers 11

708

After editing .gitignore to match the ignored files, you can do git ls-files -ci --exclude-standard to see the files that are included in the exclude lists; you can then do

  • Linux/MacOS: git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached
  • Windows (PowerShell): git ls-files -ci --exclude-standard | % { git rm --cached "$_" }
  • Windows (cmd.exe): for /F "tokens=*" %a in ('git ls-files -ci --exclude-standard') do @git rm --cached "%a"

to remove them from the repository (without deleting them from disk).

Edit: You can also add this as an alias in your .gitconfig file so you can run it anytime you like. Just add the following line under the [alias] section (modify as needed for Windows or Mac):

apply-gitignore = !git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached

(The -r flag in xargs prevents git rm from running on an empty result and printing out its usage message, but may only be supported by GNU findutils. Other versions of xargs may or may not have a similar option.)

Now you can just type git apply-gitignore in your repo, and it'll do the work for you!

16
  • 10
    I think xargs for mac has no -r... or mine is buggy/old, I don't know... and why the ! before git? well... mine one worked as alias apply-gitignore="git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached". tks! Dec 6, 2012 at 21:09
  • 20
    On Windows for /F "tokens=*" %a in ('git ls-files -ci --exclude-standard') do @git rm --cached "%a" Jul 3, 2013 at 16:07
  • This just removed my .gitignore files, not the files I wanted to ignore that were listed in the .gitignore files - was that the point?
    – Hippyjim
    Mar 21, 2014 at 7:38
  • 1
    Worked flawlessly. Much appreciated! Jun 6, 2014 at 19:11
  • 1
    For mac users, -r is default and is not accepted by BSD versions. So remove -r.
    – Weishi Z
    Apr 9, 2016 at 4:56
624
  1. Edit .gitignore to match the file you want to ignore
  2. git rm --cached /path/to/file

See also:

7
  • 20
    There's a lot of files though. I don't want to have do this for every single one
    – user880954
    Sep 23, 2011 at 11:08
  • 1
    Are the files in any common directories? If not, the problem reduces to a shell usage question, not a git one.
    – Matt Ball
    Sep 23, 2011 at 11:19
  • 15
    There does need to be a 'I forgot to add my .gitignore until after my initial commit' mode. Jul 3, 2013 at 15:58
  • 1
    'I forgot to add my .gitignore until after my initial commit' and the answer is the post below this one. It works!
    – unom
    Oct 22, 2014 at 20:19
  • 4
    It asked me to add flag -r to be able to do so.
    – Peter
    Apr 9, 2018 at 6:01
203

to leave the file in the repo but ignore future changes to it:

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

and to undo this:

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

to find out which files have been set this way:

git ls-files -v|grep '^h'

credit for the original answer to http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/

5
  • 1
    Chronial has a great answer for doing this recursively for a directory: stackoverflow.com/a/16346756/1101076
    – alana314
    Feb 26, 2016 at 20:46
  • 1
    I wanted to commit an IDE config but not track unnecessary changes to it. This works, though I'm not sure if it'll work for others who check out the repository. It can be combined with git ls-files -ci --exclude-standard -z | xargs -0 git update-index --assume-unchanged if that helps.
    – dlamblin
    May 10, 2017 at 7:17
  • 18
    This is the only answer that does precisely what the title question asks for. PERFECT! Most of these other answers were messing with files needlessly. Jun 6, 2017 at 22:34
  • 1
    This is the exactly right answer for the question. Using commands like git rm --cached will remove the file in repository, which doesn't meet the need of quizzer. Thank you. I really appreciate it. Your answer helps me a lot.
    – ramwin
    Apr 14, 2020 at 5:15
  • The last command, on windows, replace grep with findstr for the same result. git ls-files -v|findstr '^h'
    – MattyMatt
    Dec 10, 2021 at 16:31
200

Be sure that your actual repo is the latest version and without ongoing changes

  1. Edit .gitignore as you wish
  2. git rm -r --cached . (remove all files)
  3. git add . (re-add all files according to gitignore)

then commit as usual

5
  • 34
    This should be the accepted answer as the question specifically said there were a lot of files to remove.
    – nbeuchat
    Feb 1, 2018 at 12:30
  • 4
    Note that step 2 this can take a while if you have a big repository.
    – brainbag
    Jun 4, 2018 at 14:16
  • 3
    This just led to removal of all files from the index! This saved me: git reset HEAD (Remember to stash any changes)
    – aksh1618
    Nov 14, 2018 at 19:54
  • 4
    Step 2 removes everything, yes, but step 3 should readd them all, except for the ones that fail due to .gitignore.
    – ojchase
    Jan 15, 2019 at 18:19
  • This works perfectly. Jun 7 at 9:03
12

Old question, but some of us are in git-posh (powershell). This is the solution for that:

git ls-files -ci --exclude-standard | foreach { git rm --cached $_ }
0
2
  1. Add your filename/file-path into the .gitignore file.
  2. Now run command in git bash git rm --cached file/path/from/root/folder
  3. Now commit your code using git commit.

Now the file should not get tracked in the git status command.

0

I tend to forget / am too lazy to read what these git commands do. So I do it like this:

  • I move all files, which I want to ignore, temporarily outside the repository
  • I commit these "deleted files" in my git GUI (they are now untracked)
  • I update the .gitignore file
  • I move the files back into the repository (or delete them, if that was my intention)
0

İf you already committed, you can use this:

  1. Add folder path to .gitignore file.
  2. Run this command for removing exe (for another file extensions, just write "*extension") files. git rm --cached *exe
  3. Commit changes.

You should do this for every extension that you wanted to remove.

0

After editing .gitignore file.

git rm -r --cached ./ &&  git add ./ && git commit -a -m "Removing ignored files" && git push
0

If you are git rm'ing a lot of files (say, 100k), answers in this thread could be improved by processing the list of files (produced by git ls-files -ci --exclude-standard) in batches. (AFAIK, parallel processing will not work with git due to its index-locking mechanism.)

Example of batching in Powershell:

$files = git ls-files -ci --exclude-standard
$batchSize = 100

for ($i = 0; $i -lt $files.Count; $i += $batchSize) {
    $batch = $files[$i..($i + $batchSize - 1)]
    git rm -f --cached $batch
}

On Windows, the upper bound on $batchSize is the limit of the command line length. For short paths, you might get away with larger a $batchSize.

-3

Follow these steps:

  1. Add path to gitignore file

  2. Run this command

    git rm -r --cached foldername
    
  3. commit changes as usually.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.