Applying .gitignore to committed files

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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!

Solution 2 - Git

Solution 3 - Git

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/

Solution 4 - Git

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

Solution 5 - Git

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 $_ }

Solution 6 - Git

  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.

Solution 7 - Git

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)

Solution 8 - Git

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

Solution 9 - Git

Follow these steps:

  1. Add path to gitignore file

  2. Run this command

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser880954View Question on Stackoverflow
Solution 1 - GitJonathan CallenView Answer on Stackoverflow
Solution 2 - GitMatt BallView Answer on Stackoverflow
Solution 3 - Gitben.tiberius.averyView Answer on Stackoverflow
Solution 4 - GitThéo AttaliView Answer on Stackoverflow
Solution 5 - GitChris PfohlView Answer on Stackoverflow
Solution 6 - GitJayView Answer on Stackoverflow
Solution 7 - GitNils LindemannView Answer on Stackoverflow
Solution 8 - GitumutcxView Answer on Stackoverflow
Solution 9 - GitKalyan Chakravarthi VView Answer on Stackoverflow