Mittenchops
Mittenchops

Reputation: 19694

.gitignore regex for emacs temporary files

I'm trying to .gitignore emacs temporary/autosave files. I'm using...

\.\#.*

in my .gitignore.

But git add -A run in a subfolder is still giving me:

#       new file:   .#make_collections.py
#       new file:   .#norm_collections.py
#       new file:   make_collections.py
#       new file:   norm_collections.py

even though

\.\#.*

is clearly getting the right file names and not the wrong ones when I test it with a regex tester.

Upvotes: 25

Views: 17543

Answers (6)

apitsch
apitsch

Reputation: 1702

To suppress the temporary Emacs files appearing on git status globally, you can do the following:

  1. Configure git to use a global excludesfile

    Since this is a common problem, git has a specific solution to that:

    Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig

    git config --global core.excludesfile ~/.gitignore_global    
    
  2. Create the respective file

     cd
     touch .gitignore_global
    
  3. Paste the following template into the file

    # -*- mode: gitignore; -*-
    *~
    \#*\#
    /.emacs.desktop
    /.emacs.desktop.lock
    *.elc
    auto-save-list
    tramp
    .\#*
    
    # Org-mode
    .org-id-locations
    *_archive
    
    # flymake-mode
    *_flymake.*
    
    # eshell files
    /eshell/history
    /eshell/lastdir
    
    # elpa packages
    /elpa/
    
    # reftex files
    .rel
    
    # AUCTeX auto folder
    /auto/
    
    # cask packages
    .cask/
    dist/
    
    # Flycheck
    flycheck_*.el
    
    # server auth directory
    /server/
    
    # projectiles files
    .projectile
    
    # directory configuration
    .dir-locals.el
    
    # network security
    /network-security.data
    
  4. Watch git do its magic! :)

Upvotes: 4

Joe
Joe

Reputation: 1368

If you want an easy way to ignore files, you can also use http://www.gitignore.io which helps create useful .gitignore files for your project.

Here is the emacs template: https://www.gitignore.io/api/emacs

There is also documentation demonstrating how to run gi from the command line.

Upvotes: 3

Pedro Luz
Pedro Luz

Reputation: 2772

files are ignored with:

\#*\# .\#*

Upvotes: 12

user507077
user507077

Reputation:

gitignore doesn't use regular expressions. Instead it uses shell glob patters. The man page tells you two things important for this situation:

Otherwise, Git treats the pattern as a shell glob suitable for
consumption by fnmatch(3) with the FNM_PATHNAME flag.

and

A line starting with # serves as a comment. Put a backslash ("\")
in front of the first hash for patterns that begin with a hash.

This means that the pattern you want to use is simply .#*.

Now the second pattern that matov mentioned, #*, doesn't do anything as it is treated as a comment by git. Hence me quoting that second sentence from the man page.

Upvotes: 21

user2053036
user2053036

Reputation:

You can also instruct emacs to save the autosave files in a different directory altogether by setting the variable auto-save-file-name-transforms, I have this in my init file

(setq auto-save-file-name-transforms
          `((".*" ,(concat user-emacs-directory "auto-save/") t))) 

This instructs emacs to store the auto-saves inside the auto-save folder in the user-emacs-directory (usually ~/.emacs.d).

To save backup files in a different directory set the variable backup-directory-alist, the following will save backup files inside backups folder in the user-emacs-directory

(setq backup-directory-alist
      `(("." . ,(expand-file-name
                 (concat user-emacs-directory "backups")))))

Upvotes: 37

Özgür Eroğlu
Özgür Eroğlu

Reputation: 1270

Emacs autosave files are ignored with

\#*#

Upvotes: 17

Related Questions