There is a file that changes constantly when the application itself is running and you do not need to commit it. How to make it not to commit? .gitignore is not an option, since the file has already been created.

  • 3
    Need more context. What kind of file is it, why is it constantly changing, is it necessary in the gita for the application to work correctly? It may be easier to remove it completely from the repository and then add it to gitignore, since the application can work without changes in this file. - awesoon
  • 3
    just quite an option. Add this file to .gitignore and don't forget to commit .gitignore. - KoVadim
  • 2
    Than you are not satisfied with gitignore? Created - and figs with him. - free_ze
  • four
    It is necessary to add to .gitignore and make git rm --cached <filename> , the details in the question by reference. - Nick Volynkin
  • 2
    > .gitignore is not an option since the file has already been created. Why not an option? Just the same option, add the created file to .gitignore. - jekaby 2:51

2 answers 2

Suppose that you have the following file folder (repository) and a certain number of commits:

enter image description here

You find that in the repository there is a file which is absolutely not needed in the project.


We correct the error

Situation number 1. Unnecessary file already in commit

You will need to remove the file from commits first. Execute the command:

 git filter-branch --tree-filter 'rm -f NOOOO.cpp' HEAD 

Link to the original answer.

Situation number 2. Stage Unnecessary File

You executed the git add * command (for example) and the entire repository was able to stage. The git status command will show which files are in what state.

enter image description here

In this situation, you need to run git rm --cached NOOOO.cpp or git reset NOOOO.cpp . After executing the command, we get the following result:

enter image description here

Situation number 3. An unnecessary file in was created but did not come into view of git

In this situation, you need to go to the next item :).


Avoid future mistakes

Now we got rid of the unnecessary file in the commits of the existing or in the stage area. It is necessary now to ensure that git does not "see the file."

We create .gitignore (on win it is possible to create through .gitignore. ). add the following line to it and save:

 NOOOO.cpp 

Now you need to git indexed gitignore. for this we will add it to the new commit:

 git add .gitignore git commit -m "gitignore" 

After that, git will report that there are no new files:

enter image description here

Successful commits!

    If you want to stop tracking changes in any file in the git repository, but the .gitignore file doesn’t fit for some reason - for example, the file is quite specific for a particular project and does not occur on other computers (say, temporary test files, useful only to a single participant), you can use the .git/info/exclude file. It should be located in the git'a hidden internal directory, its syntax and semantics is exactly the same as that of gitignore . The only difference is that it is local to the repository, its contents are visible only to its owner.

    There is also a global version of this file. It is used for all user repositories and is located in ~/.config/git/ignore by default (the path can be changed by setting core.excludesFile ). Here it is advisable to specify the temporary files of your favorite IDE, as an example.

    For more details on the processing of all such files: classic .gitignore , as well as the aforementioned .git/info/exclude and ~/.config/git/ignore - see the gitignore (5) manual. All variations of the naming of these files are described there.