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

筅森魡賤 提交于 2019-11-28 17:50:56

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.

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>

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.

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.

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)

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