How to keep git from tracking all files that end with a "~"?

Git

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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

[1]: http://www.kernel.org/pub/software/scm/git/docs/gitignore.html "gitignore"

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncorazzaView Question on Stackoverflow
Solution 1 - GitfilmorView Answer on Stackoverflow
Solution 2 - GitruffinView Answer on Stackoverflow
Solution 3 - GitGarrett VliegerView Answer on Stackoverflow
Solution 4 - Gitofri cofriView Answer on Stackoverflow
Solution 5 - GitPoxls88View Answer on Stackoverflow