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

▼魔方 西西 提交于 2019-11-27 11:03:19

问题


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.


回答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.




回答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>




回答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.




回答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

for more details.




回答5:


Just don't commit the files you want to exclude.

For example, if you have two files a and b that are in the local repo, but you only want to commit one of them, use: git add a to add one of the files. Then commit with git commit (don't include the -a flag)



来源:https://stackoverflow.com/questions/11895712/git-is-it-possible-to-exclude-file-from-git-push-but-keep-them-in-the-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!