git - 是否可以从“git push”中排除文件,但将它们保存在本地存储库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11895712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:22:05  来源:igfitidea点击:

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

gitversion-controlrepository

提问by halloleo

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

在我的主目录中,我在本地 git 存储库中有文件,因为我想在版本控制下跟踪它们。

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 .gitignorefile, because it would exclude these files completely from being tracked.

我怎样才能用 git 实现这一目标?我可以配置一个“ .gitignore-for-push”文件吗?我不能使用本地.gitignore文件,因为它会将这些文件完全排除在跟踪之外。

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

ps:我知道问题Is there an exclude file-equivalent...,但答案是.gitignore我无法使用的路径。另一个问题在推送时排除特定文件...仅回答 git+heroku 的特定情况,而不是单独的 git。

采纳答案by dunni

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.

但是你可以为你的敏感数据使用一个子模块,并将这个子模块保留在你的本地机器上,同时你将常规的 git 存储库推送到远程机器。

回答by redhotvengeance

You can go ahead and actually track these files (sans the sensitive info), but then use:

您可以继续实际跟踪这些文件(没有敏感信息),然后使用:

git update-index --assume-unchanged <file>

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.

在每个文件上。然后,您可以继续将敏感信息添加到每个文件中,但 Git 不会将文件视为已更改,也不会尝试提交(从而推送)该敏感信息。

To get Git to update the info again, you'd use:

要让 Git 再次更新信息,您可以使用:

git update-index --no-assume-unchanged <file>

git update-index --no-assume-unchanged <file>

回答by halloleo

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.

我最终解决这个问题的方法如下:将敏感信息放在一个带有自己的 git 存储库的子目录中,并将文件符号链接回旧位置。

E.g. in your home folder (~) lives the file .credswhich you do notwant in the public repository. Move this file in a sub folder called, say protectedand create a symlink from ~to protected/.creds.Of course, do not include this folder in your ~repository , but create a newrepository in the folder protectedjust to keep track of .creds. If you do not push this repository publicly at all, you are set.

例如,在您的主文件夹 ( ~) 中存放.creds了您希望在公共存储库中的文件。将此文件移动到一个名为的子文件夹中,例如protected并从~到创建符号链接protected/.creds.当然,不要将此文件夹包含在您的~存储库中,而是在文件夹中创建一个存储库protected只是为了跟踪.creds. 如果您根本不公开推送此存储库,则您已准备就绪。

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.

我知道这个解决方案是一种逃避:我的问题指出该文件驻留在同一目录中,但符号链接对我有用。

回答by Sireesh Yarlagadda

Here git-update-index - Register file contents in the working tree to the index.

这里 git-update-index - 将工作树中的文件内容注册到索引。

git update-index --assume-unchanged <PATH_OF_THE_FILE>

Example:-

例子:-

git update-index --assume-unchanged somelocation/pom.xml

for more details.

更多细节。

回答by zjagannatha

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

只是不要提交要排除的文件。

For example, if you have two files aand bthat are in the local repo, but you only want to commit one of them, use: git add ato add one of the files. Then commit with git commit(don't include the -aflag)

例如,如果您有两个文件a并且b它们在本地存储库中,但您只想提交其中一个,请使用: git add a添加其中一个文件。然后提交git commit(不包括-a标志)