简体   繁体   English

如何让git跟踪所有以“〜”结尾的文件?

[英]How to keep git from tracking all files that end with a “~”?

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 ~. 我正在使用Gedit,每次保存文件时,Gedit都会创建一个副本,副本的名称总是以〜结尾。 The problem is, Git always tries to track these files, and I don't want that! 问题是,Git总是试图跟踪这些文件,我不希望这样! Is there a way to still be able to use git add . 有没有办法仍然可以使用git add . , but add just those files that do not end with ~? ,但只添加那些不以〜结尾的文件?

gitignore is the way to go. gitignore是要走的路。 Just add *~ to .gitignore at the root of you repo. 只需在你的repo的根目录添加*~ .gitignore

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: 如果你想用一个代字号结尾(这应该是安全的;我无法想象一个合理的用例,那就是坏的),请确保以下行位于repo文件夹顶部的.gitignore文件中层次结构:

*~

If you also want 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. 最好让Gedit将其备份文件放在其他地方。 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. JEdi​​t和VIm,我最常用的两个编辑器, 都有这样的设置 ,除了加载gitignore之外,将其保存在其他地方要清楚得多。

Unfortunately, Gedit doesn't have that option. 不幸的是,Gedit没有这个选择。 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? 这不是最坏情况 - 这就是为什么你在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. 要防止Gedit将来创建这些备份,请打开Gedit,打开“首选项”对话框(“编辑”>“首选项”),选择“编辑器”选项卡,删除“保存前创建文件的备份副本”选项,然后单击“确定”。关。 After doing this, Gedit will no longer make the backups with tildes all over the place. 完成此操作后,Gedit将不再使用波浪线进行备份。

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文件,您可以在其中放置*~规则。

Why bother adding a .gitignore file when you can edit the plaintext file .git/info/exclude . 当您可以编辑明文文件.git/info/exclude时,为什么还要添加.gitignore文件。 "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 “当决定是否忽略路径时,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存储库中的.git/info/exclude文件中的一行。 Git will ignore that pattern and all of the files ending in tildes. Git将忽略该模式以及以tildes结尾的所有文件。

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. 如果你有带扩展名的文本,比如Sketchup,它创建以“〜.skp”结尾的备份文件,那么你需要在.gitignore文件中添加*~.skp 〜.skp。 Or change skp for the extension of the software you are using. 或者更改skp以获取您正在使用的软件的扩展名。 Or use *~.* if you are sure that all files ending with tilde with all extensions are safely to be ignored. 或者使用*~.*如果您确定所有以波浪号结尾的所有文件都可以安全地被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM