GIT 忽略和 GIT 挂钩 - 提交推送后文件被替换

GIT Ignore and GIT Hook - File is replaced after a commit-push

我的 GIT 存储库位于 /var/repo/myRepo.git。我设置了一个 GIT 挂钩 post-receive* 以将文件从我的存储库复制到我的项目文件夹

git --work-tree=/var/www/laravel --git-dir=/var/repo/myRepo.git checkout -f

每次我在服务器上提交和推送一些东西时,文件 var/www/laravel/config/services.php 被替换,我在服务器上所做的修改被我的本地副本替换。

例如,如果我在服务器上像这样手动修改以下文件(通过 ssh 会话)

var/www/laravel/config/services.php

This is the modified content of this file

提交和推送后会像这样

var/www/laravel/config/services.php

This is the default content of this file

我试图将 /config/services.php 添加到我的 .gitignore 但它似乎不起作用。

.gitignore

/node_modules
/public/storage
/public/hot
/storage/*.key
/vendor
/.idea
Homestead.json
Homestead.yaml
.env
/config/services.php

我应该怎么做才能让这个文件在我每次在我的服务器上提交内容时都不会被替换?

What should I do so this file is not replaced each time I commit something on my server?

你只有两个选择:

  1. 不签入,或者
  2. 不检查。

你的git checkout -f命令意味着"get me the latest commit, overwriting everything."如果最新的提交有一个文件的新版本,它将覆盖文件的旧版本。

(此外,.gitignore 文件并不代表您认为的意思。它不是要忽略 的文件列表。它是文件列表——或者命名模式——不要抱怨。通常最重要的是,它让你声明Git:"Yes, I know these are in my work-tree and not in my index; don't tell me that."这是在输入端——即"don't check it in"部分。)

这引出了关于可配置软件的一般规则,其中软件本身在 Git 或任何版本控制系统中维护:不要将配置放入版本控制系统. 配置 不是软件的一部分。

例如,考虑一下 Git 本身。您必须 配置 Git 告诉它您的 user.nameuser.email 以便使用您的用户名和电子邮件地址进行提交。现在想象一下 Git 附带了软件内置的配置文件,其中说您的用户名是 Fred,您的电子邮件是 fred@fred.fred每次您将 Git 更新到新版本时, 都会将您的名字改回 "Fred <fred@fred.fred>"。这不太友好吧?

Git 将您的配置 存储在 Git 软件的 之外。可以肯定的是,default 配置条目,但是 you 设置的任何内容都保存在别处。更新 Git 与此无关。这不特定于 Git,甚至版本控制系统:任何提供升级的系统不得将其配置存储在被升级破坏的文件。

所以,停止这样做。

我做了 git rm /config/services.php 并手动重新导入了文件。现在文件没有被替换为 GIT.