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

后端 未结 5 1045
执笔经年
执笔经年 2020-12-24 11:16

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 fil

相关标签:
5条回答
  • 2020-12-24 11:54

    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.

    0 讨论(0)
  • 2020-12-24 11:55

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

    0 讨论(0)
  • 2020-12-24 11:55

    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.

    0 讨论(0)
  • 2020-12-24 12:06

    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.

    0 讨论(0)
  • 2020-12-24 12:18

    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.

    0 讨论(0)
提交回复
热议问题