0

I'm trying to implement an updater command into my Discord.js bot so whenever you type ;;update, it will check for the latest commit from my GitHub repository and download all the files except the ones in the .gitignore file.

I have files like config.json which are in the .gitignore file but whenever I try and pull from the repository, config.json is replaced and I lose all configuration. I only want to download the files that aren't in .gitignore.

If that was a bit of a mouthful:

  1. Person types ;;update from within the bot.
  2. Download all files from https://github.com/VenkSociety/Tsuyo (except the ones in the ignored in .gitignore).
  3. Happy days.
Roberto Murguia
  • 357
  • 1
  • 2
  • 13
Venk
  • 230
  • 2
  • 19

2 Answers2

0

How to remove a file from a repo (.gitignore by itself is not enough)

Most likely, the file that is being overwritten was added to the repository before it was added to the .gitignore file.

If the file was added to the repo in an older commit, you will need to delete the file, then add the deletion to the index, and make a new commit. This is counterintuitive at first, because you will delete the file, and then git add <file that was just deleted>, but you just need to realize that you are not adding the file itself to the index to be committed, you are only adding the new changes to this file to the index to be committed. Those changes can be adding a new file, changing an existing file, or removing that file from the repo.

Once you commit the deletion, any commit from that point forward will not have the file, and if you readd the file it will be ignored because of the .gitignore file.

Why your file keeps getting overwritten

The key here is that adding a file to .gitignore will not remove a file from the repo by itself. It will only keep git from tracking any new changes to the file. The result is that the file in the repo never changes, so even if you change it locally, all commits in the repo will have the old version of the file, so as soon as you checkout any commit, it will overwrite your changes with the old version of the file.

LightCC
  • 9,804
  • 5
  • 52
  • 92
0

The reason your files are being downloaded is because they're in your repository. It's possible to add a file to .gitignore that's already in the repository and doing so has no effect whatsoever. It does not prevent new updates to the file or prevent Git from tracking changes to the file in any way.

If the bot uses a configuration file located in config.json, the thing to do is not to provide a configuration file under that name. You should provide an example configuration file (maybe under examples/config.json) instead and either let folks copy it or copy it in automatically if you need one but it's missing. You can do this with an appropriate git mv invocation.

It isn't trivial to exclude the download of files in .gitignore because that file has wildmatch patterns and matching the files against those patterns requires a suitable library. If you're using GitHub's archive feature, that isn't something they support.

Using an example or template configuration file is a good choice for several reasons:

  • It solves the present problem: you don't overwrite people's configuration files because you don't ship a file with the same name.
  • It prevents you from the entire class of problems of trying to ignore changes to a tracked file, which is so common that Git has a FAQ on it.
  • It's the best practice for shipping configuration files in version control if folks need to modify them.

As for downloading it, you can simply use an appropriate release tarball or zipball from GitHub's archive functionality, which should meet your needs if you adopt my proposed solution. Alternatively, you can use your preferred package manager to distribute it, which should also work fine.

bk2204
  • 64,793
  • 6
  • 84
  • 100