.gitignore正则表达式为emacs临时文件

时间:2014-03-04 14:57:42

标签: regex git emacs gitignore

我正在尝试.gitignore emacs临时/自动保存文件。我正在使用......

\.\#.*

在我的.gitignore中。

git add -A在子文件夹中运行仍在给我:

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

即使

\.\#.*
当我test it with a regex tester时,

显然得到了正确的文件名而不是错误的文件名。

6 个答案:

答案 0 :(得分:27)

你也可以通过设置变量auto-save-file-name-transforms来指示emacs将自动保存文件完全保存在不同的目录中,我在我的init文件中有这个

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

这指示emacs将自动保存存储在user-emacs目录中的auto-save文件夹中(通常为~/.emacs.d)。

要将备份文件保存在另一个目录中设置变量backup-directory-alist,以下内容将把备份文件保存在user-emacs-directory

目录中的backups文件夹中
(setq backup-directory-alist
      `(("." . ,(expand-file-name
                 (concat user-emacs-directory "backups")))))

答案 1 :(得分:20)

gitignore不使用正则表达式。相反,它使用shell glob模式。 man page告诉你两件对这种情况很重要的事情:

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

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

这意味着您要使用的模式只是.#*

现在,matov提到的第二种模式#*没有做任何事情,因为它被git视为评论。因此,我引用了手册页中的第二句话。

答案 2 :(得分:9)

将忽略Emacs自动保存文件

\#*#

答案 3 :(得分:4)

文件将被忽略:

\#*\# .\#*

答案 4 :(得分:3)

如果您想要一种简单的方法来忽略文件,您还可以使用http://www.gitignore.io来帮助您为项目创建有用的.gitignore文件。

以下是emacs模板:https://www.gitignore.io/api/emacs

还有documentation演示了如何从命令行运行gi

答案 5 :(得分:1)

要禁止全局出现在git status上的临时Emacs文件,您可以执行以下操作:

  1. 配置git以使用全局排除文件

    由于这是一个常见问题,因此git有一个specific solution

    用户希望Git在所有情况下都忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常会进入用户〜/ .gitconfig中由core.excludesFile指定的文件

    git config --global core.excludesfile ~/.gitignore_global    
    
  2. 创建相应文件

     cd
     touch .gitignore_global
    
  3. 将以下template粘贴到文件中

    # -*- 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. 观看git发挥其魔力! :)