git - Is it possible to exclude file from `git push`, but keep them in the local repository?

Viewed 32854

In my home directory I have files in a local git repository, because I want track them all under version control.

Most of these files I want to push to a remote repository, but a few I want to keep in my local repository only (they contain mildly sensitive information).

How can I achieve this with git? Can I configure a ".gitignore-for-push" file? I cannot use the local .gitignore file, because it would exclude these files completely from being tracked.

ps: I am aware of question Is there an exclude file-equivalent..., but the answer goes down the .gitignore path which I cannot use. The other question Exclude specific files when pushing... answers only a specific case for git+heroku, not git alone.

7 Answers

git update-index --skip-worktree directory/fileName.js

This would modify the index or directory cache. Each file mentioned is updated into the index and any unmerged or needs updating state is cleared. For more reference, you can go through the following URL. https://git-scm.com/docs/git-update-index

Might it be simplest to create a local branch, delete the sensitive files from that branch, then only ever push that branch to the remote repo? If they rarely change, when you merge locally they should stay deleted in the new branch without asking you to confirm. Plus having a branch specifically used for that remote push gives you a clear inventory of what you are pushing simply by looking at the file system contents without worry about whether a given file or folder is really a link or submodule etc.

Related