git - Is it possible to exclude file from `git push`, but keep them in the local repository?
Solution 1
This is not possible. If you have committed a file, then it will be pushed. The push action only acts on commits, not on a file basis.
But you could use a submodule for your sensitive data and keep this submodule on your local machine, while you push the regular git repository to the remote machine.
Solution 2
You can go ahead and actually track these files (sans the sensitive info), but then use:
git update-index --assume-unchanged <file>
on each file. Then you can go ahead and add the sensitive info to each file, but Git will not see the file as changed, and not try to commit (and thus push) that sensitive info.
To get Git to update the info again, you'd use:
git update-index --no-assume-unchanged <file>
Solution 3
The way I eventually got around the issue is the following: Put the sensitive information in a sub directory with its own git repository and symlink the file(s) back to the old location.
E.g. in your home folder (~
) lives the file .creds
which you do not want in the public repository. Move this file in a sub folder called, say protected
and create a symlink from ~
to protected/.creds.
Of course, do not include this folder in your ~
repository , but create a new repository in the folder protected
just to keep track of .creds
. If you do not push this repository publicly at all, you are set.
I know this solution is kind of a cop out: My questions states that the file resides in the same directory, but the symlinking works for me.
Solution 4
Here git-update-index - Register file contents in the working tree to the index.
git update-index --assume-unchanged <PATH_OF_THE_FILE>
Example:-
git update-index --assume-unchanged somelocation/pom.xml
halloleo
Updated on December 28, 2020Comments
-
halloleo over 3 years
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. -
simont almost 12 yearsThis will prevent the files from being under version-control at all, which is at best a bad solution for most configuration files, sensitive code etc. A
git submodule
is probably a better solution -
echristopherson almost 12 yearsThis has the downside that it wouldn't track changes to the file; but it is useful to know about the option.
-
halloleo over 11 yearsYep, I suspected this beforehand. Interesting idea with the
submodule
- I will check it out. My current solution though uses a 2nd directory (see my answers stackoverflow.com/a/12019595/65889) - and with that it is kind of cheating, I know. -
ThinkBonobo almost 9 yearsI was looking for a way to ignore files locally without modifying the repository or the gitignore file (which would affect other people). I tried modifying the global git ignore config and this file under .git/info/exclude. Neither worked but this did the job. Thanks!
-
MeanGreen over 3 yearsWhile it might be an answer to the question, it's best to explain what this command does. Code-only answers are generally not accepted.