How do I ignore a file after it’s already in a commit?

Asked

Viewed 15,001 times

6

I accidentally added some images in my repository.

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

Example:

public/imagens/*

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

updating

The @Guilhermenascimento requested that I demonstrate how the archive was created .gitignore in my specific case.

He looks like this:

public/solicitacoes/**/*
public/fichas_tecnicas/**/*
public/imagens/**/*
public/xls/**/*
  • What can be improved on the question? Please comment, so I can be clearer.

  • Wallace I’m not sure, but see if this has anything to do with the problem http://answall.com/a/93135/3635 - of course I wanted to ignore most of them, but with some exceptions. I think the question is OK, just add like this the structure folder and confirm if the . gitignore is just that line, because if you have more can be a conflict of "logic".

  • @Guilhermenascimento actually has the following problem: an image folder is in the repository. Over time, many tests and many uploads, it’s gotten big. I don’t want to have it in my repository anymore, with the files being added to the commit. I want to make this folder or files ignored. But it seems that after being in the repository it doesn’t want to obey . gitignore

  • @Guilhermenascimento made a test here with a file that was already before and worked. I think I’m confused as to the functioning of . gitignore

  • I understood this Wallace and have a notion of how it works :) - I just wanted to know the folder structure, why the "order" of the exclusion of . gitignore can affect the process. Sometimes it is necessary to create Whitelist after blackslist (I don’t know if this is the term), also note that if you have more files to ignore at a higher level than public/images can be a conflict of rules. I just really wanted a confirmation that public/imagens/* is the only rule.

  • 1

    It looks like . gitignore will ignore only files that have not been tracked(tracked files). Once you’ve added the files to the repository (git add) try running the command below and make sure it’s ignored (source: http://stackoverflow.com/questions/4308610/how-to-ignore-certain-files-in-git): **git rm filename --cached ** If resolved, referred to as reply.

  • Only one note this ignores files inside other folders inside images public/imagens/**/* and not the image folder itself, I mean.

  • Possible duplicate of How do I remove a folder from Git history?, in that response is used git filter-branch, that rewrites the entire history and is not a recommended procedure for public repositories, as it will be necessary to re-clone the project

  • Wait I don’t understand, I thought the problem was to avoid adding and not removing old

Show 4 more comments

2 answers

9


The .gitignore serves to ignore only untraceable files, ie from the moment when a git add is used to track file changes, the .gitignore cannot 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. (https://git-scm.com/docs/gitignore)

If you haven’t done the commit still, use:

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

If you have ever performed one commit, utilize:

  • 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 name is Given.

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

There’s also a third situation that might interest some, where git keeps a copy of the file and stops updating 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-]assume-unchanged
When this flag is specified, the Object Names Recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume 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 (e.g. cifs).

Git will fail (gracefully) in case it needs to Modify this file in the index e.g. when merging in a commit; Thus, in case the assumed-untracked file is changed upstream, you will need to Handle the Situation Manually.

Source: https://git-scm.com/docs

2

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

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

#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 git status from there, your file won’t appear as something to go up to git.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.