【问题标题】:.gitignore regex for emacs temporary files.gitignore 正则表达式用于 emacs 临时文件
【发布时间】:2014-03-04 14:57:42
【问题描述】:

我正在尝试 .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 时,显然得到了正确的文件名而不是错误的文件名。

【问题讨论】:

  • git add .(没有-A)会发生什么?
  • 它仍然会选择“.#filenames”进行添加,但现在也不会捕获删除。
  • 严格来说,.gitignore 模式实际上是正则表达式吗?我们应该看看stackoverflow.com/questions/11498059吗?

标签: regex git emacs gitignore


【解决方案1】:

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

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

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

要将备份文件保存在不同的目录中,请设置变量backup-directory-alist,以下将备份文件保存在用户emacs目录中的backups文件夹中

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

【讨论】:

  • 这是我今后将使用的问题的解决方案。
【解决方案2】:

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 视为注释。因此我引用了手册页中的第二句话。

【讨论】:

  • 它不适用于像#myfile#这样的临时文件。解决方案如下:\#*#
【解决方案3】:

使用

忽略 Emacs 自动保存文件
\#*#

【讨论】:

  • 我已将此行添加到我的 .gitignore 中,文件仍在出现。
  • 你试过了吗[#]*[#]
  • 是的,这也在我的 .gitignore 中。它确实排除了#filename.py#。只是不是.#filename.py# 前面有点。
  • 还在那里,这很奇怪。
  • .[#]*[#] 我用我的 gitignore 文件尝试了这个,它正在工作。
【解决方案4】:

文件被忽略:

\#*\# .\#*

【讨论】:

    【解决方案5】:

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

    这里是 emacs 模板:https://www.gitignore.io/api/emacs

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

    【讨论】:

      【解决方案6】:

      要全局禁止出现在 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 发挥它的魔力! :)

      【讨论】:

        猜你喜欢
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多