如何阻止 Git 跟踪此提交后对文件的任何更改?

我有一个数据库配置文件,其默认值不重要。但是,对该文件的任何更改都将包含不应在回购协议中跟踪的敏感信息。

我希望 Git 存储库的未来版本包含默认版本,但是忽略任何用户所做的任何更改。

下面的代码保留了一个本地配置,但是将删除操作推到了回购操作上,从而导致以后的回购操作出现问题。

cat "app/dir/config.file" >> .gitignore
git rm --cached app/dir/config.file

下面的步骤可以完成这项工作,但是不会持续到推送回购之后。

git update-index --assume-unchanged app/dir/config.file

这似乎是围绕敏感信息进行版本控制的常见需求,但我似乎找不到解决方案。

34591 次浏览

As usual github has a great doc on this.

https://help.github.com/articles/ignoring-files#ignoring-versioned-files

Here's the relevant snippet:

Ignoring versioned files

Some files in a repository change often but are rarely committed. Usually, these are various local configuration files that are edited, but should never be committed upstream. Git lets you ignore those files by assuming they are unchanged.

  1. In Terminal, navigate to the location of your Git repository.
  2. Run the following command in your terminal:

git update-index --assume-unchanged path/to/file.txt

Once you mark a file like this, Git completely ignores any changes on it. It will never show up when running git status or git diff, nor will it ever be committed.

To make Git track the file again, simply run:

git update-index --no-assume-unchanged path/to/file.txt.

Not sure if this is the "best" way... but what I have found to work takes a few steps.

New Laravel Example:

  1. git init
  2. Adjust .gitignore files for desired results like not losing the vendors folders.
  3. Make a copy of the files you don't want tracked (ex: .htaccess, .env, etc)
  4. git add .
  5. git commit -m "first commit"
  6. git rm --cached public /.htaccess then git rm --cached .env
  7. git commit
  8. git status should show those files as deleted.
  9. Now put the file you copied back. Since they are the same name as what you told git to remove from tracking, git will now fully ignore those files.

Now those can be edited, and you can have local and production versions. NOTE: You may have to repeat all these steps the first time on the production set-up as well. So far this has worked well for me.