如何防止 git 跟踪所有以“~”结尾的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11829708/
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
How to keep git from tracking all files that end with a "~"?
提问by corazza
I'm using Gedit, and each time I save a file, Gedit creates a copy of it, and the name of the copy always ends with a ~. The problem is, Git always tries to track these files, and I don't want that! Is there a way to still be able to use git add .
, but add just those files that do not end with ~?
我正在使用 Gedit,每次保存文件时,Gedit 都会创建一个副本,副本的名称总是以 ~ 结尾。问题是,Git 总是试图跟踪这些文件,而我不希望那样!有没有办法仍然可以使用git add .
,但只添加那些不以 ~ 结尾的文件?
采纳答案by ruffin
You want a gitignore file.
你想要一个gitignore 文件。
If you want to nuke everything that ends with a tilde (which should be safe; I can't imagine a reasonable use-case where that's bad), make sure the following line is in your .gitignore
file at the top of your repo's folder hierarchy:
如果您想取消以波浪号结尾的所有内容(这应该是安全的;我无法想象一个合理的用例,其中不好),请确保以下行在您的.gitignore
文件中位于您的 repo 文件夹层次结构的顶部:
*~
If you alsowant to get rid of those tilde files laying around in your local file system, you can. It'd be best to make Gedit put its backup files somewhere else. JEdit and VIm, the two editors I use most, have such settings, and it's lots cleaner to keep those somewhere else than loading up gitignore.
如果你也想摆脱那些波浪号文件的本地文件系统奠定左右,你可以。最好让 Gedit 将其备份文件放在其他地方。我最常用的两个编辑器 JEdit 和 VIm都有这样的设置,将它们放在其他地方比加载 gitignore 更干净。
Unfortunately, Gedit doesn't have that option. The best it can do is to turn off the ~
backups. Before you get worried, the worst case is that you lose what was in the file immediately before you saved.That's not a worst-case -- that's why you've got this in a git repo, right?
不幸的是,Gedit 没有这个选项。它可以做的最好的事情就是关闭~
备份。在您担心之前,最糟糕的情况是您在保存. 这不是最坏的情况——这就是为什么你在 git repo 中得到它,对吧?
NOTE: If you want to keep the ~
suffixed files locally, do. The .gitignore you set up, above, will keep you from accidentally sharing them.
注意:如果您想在~
本地保留后缀文件,请执行。您在上面设置的 .gitignore 将防止您意外共享它们。
You can turn off ~
suffixed backups like this
To prevent Gedit from creating these backups in the future, open up Gedit, open up the Preferences dialog (Edit > Preferences), select the Editor tab, remove the check in the “Create a backup copy of files before saving” option, and click Close. After doing this, Gedit will no longer make the backups with tildes all over the place.
为了防止 Gedit 以后创建这些备份,打开 Gedit,打开首选项对话框(编辑 > 首选项),选择编辑器选项卡,取消选中“保存前创建文件的备份副本”选项,然后单击关闭。这样做之后,Gedit 将不再在所有地方使用波浪线进行备份。
回答by filmor
回答by Garrett Vlieger
To add on to what @filmor said, you can create a global gitignore file so that all repositories will ignore the backup files:
要补充@filmor 所说的内容,您可以创建一个全局 gitignore 文件,以便所有存储库都将忽略备份文件:
git config --global core.excludesfile ~/.gitignore_global
This will tell git to look in your $HOME path for a .gitignore_global
file, which is where you can place the *~
rule.
这将告诉 git 在您的 $HOME 路径中查找.gitignore_global
文件,您可以在该路径中放置*~
规则。
回答by ofri cofri
Just complementing the answers:
If you have tilde files with extension, like Sketchup, which creates backup files ending with "~.skp", then you need to add *~.skp
in your .gitignore file. Orchange skp
for the extension of the software you are using. Oruse *~.*
if you are sure that all files ending with tilde with all extensions are safely to be ignored.
只是补充答案:
如果您有带扩展名的波浪号文件,例如 Sketchup,它会创建以“~.skp”结尾的备份文件,那么您需要添加*~.skp
.gitignore 文件。或者更改skp
您正在使用的软件的扩展名。或者使用*~.*
,如果你是确保所有文件与波浪结束与所有分机都安全地被忽略。
回答by Poxls88
Why bother adding a .gitignore
file when you can edit the plaintext file .git/info/exclude
. "When deciding whether to ignore a path, git normally checks gitignore patterns from multiple sources" https://www.kernel.org/pub/software/scm/git/docs/gitignore.html
.gitignore
当您可以编辑纯文本文件时,为什么还要费心添加文件.git/info/exclude
。“在决定是否忽略路径时,git 通常会检查来自多个来源的 gitignore 模式” https://www.kernel.org/pub/software/scm/git/docs/gitignore.html
If you add *~
to a line in the .git/info/exclude
file in your git repository. Git will ignore that pattern and all of the files ending in tildes.
如果您在 git 存储库*~
中的.git/info/exclude
文件中添加一行。Git 将忽略该模式以及所有以波浪号结尾的文件。