0

Situation

Using gedit 3.18.3 with Ubuntu 16.04 LTS.

My .git/info/exclude file is

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments. # For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# FILES
*.[oa]
*~
*.log
Makefile*

So I expect that files matching the pattern *~ are disregarded.

Issue

Yet, these are kept in the count

git status 

On branch v2
Your branch is up-to-date with 'origin/v2'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified:   README.md
modified:   README.md~

no changes added to commit (use "git add" and/or "git commit -a")

This behaviour is confirmed by

git ls-files --cached --ignored --exclude-standard

as suggested in Git: List files that should be ignored, but are not

Question

I would not want that README.md~ or any other *~ file shows up in the list.
This used to work like a breeze, but I cannot pin down a moment where it went wrong.
Any tips and workarounds please?

Community
  • 1
  • 1
XavierStuvw
  • 1,294
  • 2
  • 15
  • 30
  • 1
    https://stackoverflow.com/a/1274447/7976758 — The answer(s) talk about `.gitignore` but they're true for any git exclusion mechanism — global `.gitignore` or `.git/info/exclude`. – phd May 25 '20 at 12:21

2 Answers2

1

Since

modified:   README.md

appears in your git status, I can see you already committed the file before. It will only be ignored, if not already versioned, so you have to delete it first.

Why the file was versioned to begin with? Could be:

  • File was added before the exclude file existed (or the excluding pattern was added)
  • It was forcefully added (git add -f)
  • An other contributor added the file (see remark about .git/info/exclude below)

EDIT: see example of how to remove a file from versioning, but keep a local copy: https://stackoverflow.com/a/1143800/1050264

also see difference between .gitignore (versioned) and .git/info/exclude (local only). The latter would not ignore the files for other clones of the repository: https://stackoverflow.com/a/22906964/1050264

v01pe
  • 1,096
  • 2
  • 11
  • 19
  • I do acknowledge that once I delete this `*.~` file and I modify it again, it is not counted in by git status again. However, if the file has been committed before, it's because the exclude file had been ignored. The root problem to tackle is that git ignored its own exclude-listed files at some point earlier. Is there any error in the info I shared perhaps? In other words: I can fix this one file manually, but I expect that git follows the rule given in the exclude file, especially when this involves several directories and files – XavierStuvw May 25 '20 at 10:54
  • Regardless of .gitignore file, you can always forcefully add a file to git using `git add -f`. – 1615903 May 25 '20 at 11:14
  • 3
    Hmm… could be, that the file was added before the ignore file existed, it was forcefully added, or committed by someone else (if the repo is shared) – note that `.git/info/exclude` is only used for your clone: https://stackoverflow.com/questions/22906851/when-would-you-use-git-info-exclude-instead-of-gitignore-to-exclude-files – v01pe May 25 '20 at 11:19
1

Many people seem to have a wrong idea about what the gitignore file does.

  • It doesn’t stop you from adding a file.

  • And it doesn’t stop a tracked file from being tracked.

It just stops git from assuming the file needs to be tracked, if it isn’t already in the index. For example, if you’ve never added anything to this repo and you say git add ., this file is not added.

But once you have added it, that’s the end, git is now tracking this file. It’s in the index and that’s all that matters.

matt
  • 515,959
  • 87
  • 875
  • 1,141