git 忽略 vim 临时文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4824188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git ignore vim temporary files
提问by jrdioko
What is the correct way to make git ignore temporary files produced by vim in all directories (either globally across the system or locally for a single project)?
使 git 忽略所有目录中 vim 生成的临时文件的正确方法是什么(无论是整个系统的全局文件还是单个项目的本地文件)?
回答by nunopolonia
Vim temporary files end with ~ so you can add to the file .gitignore
the line
Vim 临时文件以 ~ 结尾,因此您可以在文件中添加.gitignore
以下行
*~
Vim also creates swap files that have the swp and swo extensions. to remove those use the lines:
Vim 还创建具有 swp 和 swo 扩展名的交换文件。删除那些使用以下行:
*.swp
*.swo
This will ignore all the vim temporary files in a single project
这将忽略单个项目中的所有 vim 临时文件
If you want to do it globally, you can create a .gitignore file in your home (you can give it other name or location), and use the following command:
如果你想全局执行,你可以在你的家中创建一个 .gitignore 文件(你可以给它其他名称或位置),并使用以下命令:
git config --global core.excludesfile ~/.gitignore
Then you just need to add the files you want to ignore to that file
然后你只需要将要忽略的文件添加到该文件中
回答by martinl
回答by William Pursell
This is something that should only be done on a per-user basis, not per-repository. If Joe uses emacs, he will want to have emacs backup files ignored, but Betty (who uses vi) will want vi backup files ignored (in many cases, they are similar, but there are about 24,893 common editors in existence and it is pretty ridiculous to try to ignore all of the various backup extensions.)
这应该只在每个用户的基础上完成,而不是每个存储库。如果 Joe 使用 emacs,他会希望忽略 emacs 备份文件,但 Betty(使用 vi 的人)会希望忽略 vi 备份文件(在许多情况下,它们是相似的,但是大约有 24,893 个常见的编辑器存在,而且很漂亮试图忽略所有各种备份扩展是荒谬的。)
In other words, do not put anything in .gitignore
or in core.excludes
in $GIT_DIR/config
. Put the info in $HOME/.gitconfig
instead (as nunopolonia suggests with --global
.) Note that "global" means per-user, not per-system.
换句话说,不要在 in.gitignore
或 incore.excludes
中放入任何东西$GIT_DIR/config
。$HOME/.gitconfig
改为输入信息(如 nunopolonia 建议使用--global
.)请注意,“全局”表示每个用户,而不是每个系统。
If you want configuration across the system for all users (which you don't), you'll need a different mechanism. (Possibly with templates setup prior to initialization of the repository.)
如果您希望为所有用户(您不需要)在整个系统中进行配置,您将需要一种不同的机制。(可能在存储库初始化之前设置了模板。)
回答by Anastasios Andronidis
I would also recommend to think to ignore files like:
我还建议考虑忽略以下文件:
.*.swp
.*.swo
as you may have files that end with .swp
因为您可能有以 .swp
回答by rouble
Hereis the actual VIM code that generates the swap file extensions:
这是生成交换文件扩展名的实际 VIM 代码:
/*
* Change the ".swp" extension to find another file that can be used.
* First decrement the last char: ".swo", ".swn", etc.
* If that still isn't enough decrement the last but one char: ".svz"
* Can happen when editing many "No Name" buffers.
*/
if (fname[n - 1] == 'a') /* ".s?a" */
{
if (fname[n - 2] == 'a') /* ".saa": tried enough, give up */
{
EMSG(_("E326: Too many swap files found"));
vim_free(fname);
fname = NULL;
break;
}
--fname[n - 2]; /* ".svz", ".suz", etc. */
fname[n - 1] = 'z' + 1;
}
--fname[n - 1]; /* ".swo", ".swn", etc. */
This will generate swap files of the format:
这将生成以下格式的交换文件:
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
Which is pretty much what is included in github's own gitignore file for VIM.
这几乎是包含在 github 自己的VIMgitignore 文件中的内容。
As others have correctly noted, this .gitignore will also ignore .svgimage files and .swfadobe flash files.
回答by Andrew_1510
Quit vim before "git commit".
在“git commit”之前退出 vim。
to make vim
use other folders for backup files, (/tmp
for example):
将vim
其他文件夹用于备份文件,(/tmp
例如):
set bdir-=.
set bdir+=/tmp
to make vim stop using current folder for .swp files:
使 vim 停止使用 .swp 文件的当前文件夹:
set dir-=.
set dir+=/tmp
Use -=, +=
would be generally good, because vim has other defaults for bdir, dir, we don't want to clear all. Check vim help for more about bdir, dir:
使用-=, +=
通常会很好,因为 vim 对bdir, dir有其他默认值,我们不想全部清除。查看 vim 帮助以获取更多关于bdir, dir 的信息:
:h bdir
:h dir
回答by SnapShot
回答by user31986
# VIM: Temperory files
*~
# VIM: Swap-files
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# VIM: Commands :cs, :ctags
tags
cscope.*
# VIM session
Session.vim
# VIM: netrw.vim: Network oriented reading, writing, browsing (eg: ftp scp)
.netrwhist
The name of the swap file is normally the same as the file you are editing, with the extension ".swp".
- On Unix, a '.' is prepended to swap file names in the same directory as the edited file. This avoids that the swap file shows up in a directory listing.
- On MS-DOS machines and when the 'shortname' option is on, any '.' in the original file name is replaced with '_'.
- If this file already exists (e.g., when you are recovering from a crash) a warning is given and another extension is used, ".swo", ".swn", etc.
- An existing file will never be overwritten.
- The swap file is deleted as soon as Vim stops editing the file.
The replacement of '.' with '_' is done to avoid problems with MS-DOS compatible filesystems (e.g., crossdos, multidos).
交换文件的名称通常与您正在编辑的文件相同,扩展名为“.swp”。
- 在 Unix 上,一个 '.' 预置在与编辑文件相同的目录中交换文件名。这避免了交换文件出现在目录列表中。
- 在 MS-DOS 机器上,当 'shortname' 选项打开时,任何 '.' 在原始文件名中替换为“_”。
- 如果此文件已存在(例如,当您从崩溃中恢复时)会发出警告并使用另一个扩展名,如“.swo”、“.swn”等。
- 现有文件永远不会被覆盖。
- 一旦 Vim 停止编辑文件,交换文件就会被删除。
'.' 的替换 使用'_' 是为了避免与MS-DOS 兼容的文件系统(例如crossdos、multidos)出现问题。
http://vimdoc.sourceforge.net/htmldoc/recover.html
http://vimdoc.sourceforge.net/htmldoc/recover.html
回答by forellana
sure,
当然,
just have to create a ".gitignore" on the home directory of your project and have to contain
只需要在项目的主目录上创建一个“.gitignore”并且必须包含
*.swp
that's it
就是这样
in one command
在一个命令中
project-home-directory$ echo '*.swp' >> .gitignore
回答by ddwolf
In myy case the temporary files are already commited by previous actions, so modifying .gitignore will not affect those commited files..., you have to git rm files_to_be_ignored --cache
first, then commit, then DONE.
在我的情况下,临时文件已经由先前的操作git rm files_to_be_ignored --cache
提交,因此修改 .gitignore 不会影响那些提交的文件......,您必须先提交,然后提交,然后完成。