简体   繁体   English

执行 git pull 时如何忽略 Github 上的文件

[英]How to ignore a file on Github when doing a git pull

I am using Ruby on Rails 4, Ruby 2.2.2, and deploying to Heroku.我正在使用 Ruby on Rails 4、Ruby 2.2.2,并部署到 Heroku。 I have my secret variables configured with Heroku config:set and I want to use Rails.application.secrets.secret_key to access my secrets.我用 Heroku config:set 配置了我的秘密变量,我想使用 Rails.application.secrets.secret_key 来访问我的秘密。

What is the best way to manage my config/secrets file with Git and GitHub while developing and deploying to Heroku?在开发和部署到 Heroku 时,使用 Git 和 GitHub 管理我的 config/secrets 文件的最佳方法是什么? I have my config file as follows:我的配置文件如下:

development:
  secret_key_base:    <value>

test:
  secret_key_base:    <value>

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base:           <%= ENV["SECRET_KEY_BASE"] %>
  # Amazon credentials
  s3_bucket_name:            <%= ENV["S3_BUCKET_NAME"] %>
  aws_secret_access_key:     <%= ENV["AWS_SECRET_ACCESS_KEY"] %>
  aws_access_key_id:         <%= ENV["AWS_ACCESS_KEY_ID"] %>
  aws_region:                <%= ENV["AWS_REGION"] %>
  aws_cloud_front:           <%= ENV["AWS_CLOUD_FRONT"] %>

  # Twitter credentials
  twilio_token: <%= ENV["TWILIO_TOKEN"] %>
  twilio_sid:   <%= ENV["TWILIO_SID"] %>
  twilio_number: <%= ENV["TWILIO_NUMBER"] %>

I had pushed changes to my file even with it in the .gitignore file because it had been tracked by Git in the past.我已经将更改推送到我的文件,即使它在 .gitignore 文件中也是如此,因为它过去曾被 Git 跟踪过。 I stopped tracking my config/secrets.yml file on Git, so I don't push changes, but when I pull down from master my config/secrets file is overridden with only the production credentials.我停止在 Git 上跟踪我的 config/secrets.yml 文件,所以我不推送更改,但是当我从 master 下拉时,我的 config/secrets 文件仅被生产凭据覆盖。

Completely removing a file from a git repository can be done, but it is tedious.可以从 git 存储库中完全删除文件,但很乏味。

You can easily remove it from the current files, though.不过,您可以轻松地将其从当前文件中删除。 Delete it with git rm yourfile and commit and push that.git rm yourfile删除它并提交并推送它。 That will delete your local file, so you might want to copy it first.这将删除您的本地文件,因此您可能需要先复制它。 You can add it back after you commited the deletion.您可以在提交删除后重新添加它。

Note that .gitignore does not concern files that are already under version control.请注意, .gitignore 不涉及已经在版本控制下的文件。 Explicitly removing such files works however.但是,明确删除此类文件是有效的。 The copy should not be tracked any more.不应再跟踪该副本。

Git doesn't provide a means for partial pulls however in your situation all you need to do is define your environment variable keys with the same name for both your production and your development. Git 不提供部分拉取的方法,但是在您的情况下,您需要做的就是为您的生产和开发定义具有相同名称的环境变量键。 Doing so will allow you to leave the file alone and utilize the values keys on both your local and your production environments without making changes any changes.这样做将允许您单独保留文件并在本地和生产环境中使用值键,而无需进行任何更改。 If you want to push new commits without pushing any changes to this file you can do that by using如果您想推送新提交而不推送对此文件的任何更改,您可以使用

git update-index --asume-unchanged <filenameHere>

This will allow you to keep this file in your local without pushing it to git with the rest of your changes.这将允许您将此文件保留在本地,而无需将其与其余更改一起推送到 git。 Unfortunately there's no way I know of to do the opposite and exclude certain files on the pull.不幸的是,我知道没有办法做相反的事情并排除某些文件。

If there are secrets in the file that you have committed that you cannot easily change / rotate, there are a couple of ways to completely remove previously committed sensitive information from git.如果您提交的文件中存在无法轻松更改/轮换的机密,则有几种方法可以从 git 中完全删除以前提交的敏感信息。

The Github guide has a good article for this. Github 指南对此有一篇很好的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM