I have a directory in a private but shared GitHub project that contains sensitive data that I want to commit locally so that it gets backed up when I back up my local hard drive, but is not pushed to the remote branch. Is there a way to set this up using Git facilities/features?
Using the .gitignore file doesn't work because as far as I can tell, there is no way to have different behavior or different .gitignore files for local commits vs. what gets pushed to the remote branch.
If this is not possible, how are you that have the same issue handling this?
Note, this is not data that I can put in ENVIRONMENT variables. It is a set of files that contain sensitive data.
You can create another local branch that will not be tracking a remote branch and commit your sensitive files on that local branch.
git checkout -b localOnly
git add sensitiveFile
git commit
git checkout originalBranch
git push (this pushes originalBranch but not localOnly)
You can also use a .gitignore file to help ensure you don't accidentally add the sensitive files to a branch that will be pushed.
Note that git may remove the sensitiveFile from your worktree when you switch back to originalBranch. In this case you can get it back into your worktree:
git checkout localOnly sensitiveFile