5

Q: where do untracked .#files come from when working in a git repository?

When working on file example.txt in a git repository, I occasionally get an untracked file showing up in status titled .#example.txt (that is, dot-hash-filename). (This happens both in magit's status buffer and when running git status from the command line, so it seems to be Emacs-related but not magit-related.)

What is causing the appearance of untracked files of this sort? It does not appear to be a standard auto-save file (or at least I've never seen the .# prefix before.)

Dan
  • 32,584
  • 6
  • 98
  • 168

2 Answers2

8

Those are lock files.

When two users edit the same file at the same time, they are likely to interfere with each other. Emacs tries to prevent this situation from arising by recording a file lock when a file is being modified. Emacs can then detect the first attempt to modify a buffer visiting a file that is locked by another Emacs job, and ask the user what to do. The file lock is really a file, a symbolic link with a special name, stored in the same directory as the file you are editing. (On file systems that do not support symbolic links, a regular file is used.)

To disable:

(setq create-lockfiles nil)

How I knew this:

I've seen these before, but I couldn't remember what they were or how I disabled them. A simple search in the Emacs source code found the file src/filelock.c, which reminded me:

ag --cc '\.#'
nanny
  • 5,704
  • 18
  • 38
  • 6
    I should add that you probably _shouldn't_ disable lockfiles. They've been added to Emacs for a reason, the same reason that lockfiles were implemented for vim, LibreOffice, Microsoft Office and a host of other programs. Working around them is probably better than banishing them. You could easily just `.gitignore` them for example. – PythonNut Feb 13 '15 at 00:03
  • @PythonNut you should post the gitignore piece as an answer. That's exactly the right idea. – Alan Shutko Feb 13 '15 at 03:54
  • @AlanShutko my reservation is that it really doesn't answer the OP's _question_ (i.e. "what are these things and where do they come from"). It's really just a fragment of advice. – PythonNut Feb 13 '15 at 04:23
  • 1
    Nice explanation, particularly because you explained how you found the answer. – Dan Feb 13 '15 at 14:37
  • @PythonNut: your point about `.gitignore` is probably the better way to deal with the lockfiles than turning them off. Although I'm accepting nanny's answer because it answers the question I posed, I'd be happy to +1 your comment if you change it to an answer as well. – Dan Feb 13 '15 at 14:39
  • .gitignore is not always a viable approach - for example, (as a JS dev), if you use mocha for testing, and are watching a directory then it will attempt to run the lock file as a test. – sunwukung Jul 22 '15 at 16:12
5

Nanny's answer is correct, but I'd like to add that disabling lockfiles is not recommended.

They've been added to Emacs for a reason, the same reason that lockfiles were implemented for vim, LibreOffice, Microsoft Office and a host of other programs. Multiple programs editing the same file can be a real pain, leading to mass confusion and possibly nuclear war (okay, maybe not). Rather than disabling the feature, I would suggest working around the files instead.

For example, you can easily add the line

.#*

to your .gitignore if they annoy you.

PythonNut
  • 10,243
  • 2
  • 29
  • 75