Is there a way of marking a file as uncommitable with Git so that it
cannot appear on GitHub?
First, there is no way to have some files and commits visible in your local Git repository but somehow not viewable in GitHub; if you have a file committed in Git it will show up in GitHub.
Second, there is no simple and practical way to ever mark an individual file itself as being “uncommitable.” But there is definitely a way to ignore a file in a Git repo: By adding the file(s)—including their relative path if needed—to a
.gitignore file specifies intentionally untracked files that Git
should ignore. Files already tracked by Git are not affected; see the
NOTES below for details.
Creating a basic
.gitignore is fairly easy since it’s just a plain text file. So—for example—if I had a
config.php file in your root you would do this; assuming you are using PHP but the concept applies for any setup. Also I am using Nano as my text editor in this example but feel free to use whatever text editor you normally use for this:
And just add that filename to that file:
Save it and now Git will simply ignore that file.
That said, what I like to do for setups like this is to keep a sample/example config neutered of sensitive specifics in the repository so I have some reference as to what the config file format is a file named something like this:
That way you know exactly how the
config.php file should be setup via
config.SAMPLE.php and you can ensure that the actual
config.php is never touched by Git.
Also, if you plan on showing off your code, you need to expect that someone will try to take that code and implement it on their own system in some way. Remember, we are not you and without a sample config file in your repo, folks won’t really understand how to implement the code on their own. Heck they might might even think you’re not competent because you didn’t provide a basic configuration example.