How to ignore a file after it has already been committed? [duplicate]

5

I accidentally added some images to my repository.

But now I want to ignore them, but you are not obeying the data that was added in .gitignore

Example:

public/imagens/*

How do I get a file or folder, which is already part of the previous commits, ignored in the current repository?

update

@GuilhermeNascimento asked me to demonstrate how the .gitignore file was created in my case.

It looks like this:

public/solicitacoes/**/*
public/fichas_tecnicas/**/*
public/imagens/**/*
public/xls/**/*
    
asked by anonymous 16.02.2016 / 14:53

2 answers

6

.gitignore is used to ignore only non-crawled files, that is, since a git add is used to track file changes, .gitignore can not ignore these files.

  

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details. ( link )

If you have not done commit yet, use:

  • git reset <arquivo-ou-diretório>

If you've already done commit , use:

  • git rm --cached <arquivo>
  • git rm -r --cached <diretório>
  

git-rm - Remove files from the working tree and from the index

     

-r
  Allow recursive removal when a leading directory is given.

     

- cached
  Use this option to unstage and remove paths only from the index. Working   tree files, if modified or not, will be left alone.

There is also a third situation that may be of interest to some, where git maintains a copy of the file and to perform updates on it. (useful for maintaining a template file when working as a team)

  • git update-index --assume-unchanged <arquivo-ou-diretório>
  

git-update-index - Register file contents in the working tree to the index

     

- [no-] assumes-unchanged
     When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets / unsets the "takes unchanged" bit for the paths. When the "assumes unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat (2) system call (eg cifs).

     

Git will fail (gracefully) in case it needs to modify this file in the index eg when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

Source: link

    
16.02.2016 / 17:20
2

For git to ignore a particular file, this file can not be in the repository.

If you previously committed it and you want to remove it, you have to remove it from git for gitignore to start working.

#gitignore
....
arquivo_a_ser_ignorado.txt
....

git rm caminho/arquivo_a_ser_ignorado.txt
git commit -m "removendo arquivo"
git push origin master

If you give a git status from there, your file will not appear as something to upload pro git.

    
16.02.2016 / 16:31