简体   繁体   中英

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 ~. 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 ~?

gitignore is the way to go. Just add *~ to .gitignore at the root of you repo.

You want a gitignore file .

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:

*~

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. 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.

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?

NOTE: If you want to keep the ~ suffixed files locally, do. The .gitignore you set up, above, will keep you from accidentally sharing them.

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.

To add on to what @filmor said, you can create a global gitignore file so that all repositories will ignore the backup files:

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.

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

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.

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. Or change skp for the extension of the software you are using. Or use *~.* if you are sure that all files ending with tilde with all extensions are safely to be ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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