简体   繁体   English

git忽略并取消跟踪远程服务器上的文件夹

[英]git ignore and untrack folder on remote server

I know similar questions have been asked multiple times, but I cannot get it to work for my case. 我知道已经多次问过类似的问题,但是我无法解决这个问题。

Where I am: I have a local repo that I push to a live server, that included a /content folder and its subfolders. 我所在的位置:我有一个本地存储库,可将其推送到实时服务器,其中包括一个/ content文件夹及其子文件夹。 During production that was okay, since I could make changes to /content and just push them. 在生产过程中还可以,因为我可以对/ content进行更改并直接推送它们。 But since this is a website using a CMS all changes to /content will now be produced remotely. 但是,由于这是一个使用CMS的网站,因此/ content的所有更改现在都可以远程生成。

Where I want to be: I now want to 'detach' /content and its subfolders from the code base, so that I can delete the hefty /content folder locally without it being deleted on the live server. 我想去的地方:现在,我想从代码库中“分离” / content及其子文件夹,以便可以在本地删除庞大的/ content文件夹,而无需在实时服务器上将其删除。 That way I still would be able to make changes in the code and push them to the server, while people are able to use the CMS to create files and changes in the /content folder. 这样,我仍然可以在代码中进行更改并将其推送到服务器,而人们可以使用CMS在/ content文件夹中创建文件和更改。

So what I did so far: 所以我到目前为止所做的是:

  1. added the folder to .gitignore: 将文件夹添加到.gitignore:

/content/

  1. removed the folder from the index: 从索引中删除了该文件夹:

git rm -r --cached content/*

  1. tried to set assume-unchanged so that on push, the files in /content would not be deleted from the live server: 尝试设置假设不变,以便在推送时,/ content中的文件不会从实时服务器中删除:

git update-index --skip-worktree

When I do this, I of course get a: 当我这样做时,我当然会得到:

fatal: Unable to mark file 严重的:无法标记文件

Since the files are in .gitignore, and they need to stay there, such that the folder will be ignored in the future. 由于文件位于.gitignore中,因此需要将其保留在该目录中,以便将来将忽略该文件夹。 So what do I do? 那我该怎么办?

Updated to reflect correction from OP, that the updates had been done on the local clone rather than the server, and that the server is a bare repo with a hook that updates the working folder via checkout -f 已更新以反映OP的更正,更新是在本地克隆而不是服务器上完成的,并且服务器是带有挂钩的裸仓库,该挂钩通过checkout -f更新了工作文件夹。


The index flags ( skip-worktree or assume-unchanged ) are not the solution to the problem, but your understanding of why (that it has anything to do with .gitignore ) is not correct either. 索引标志( skip-worktreeskip-worktree assume-unchanged )不是解决问题的方法,但是您对原因(与.gitignore无关)的理解也不正确。 Rather, those flags are set on the file's entry in the index; 而是,这些标志设置在索引中文件的条目上。 you can't set the flag on those files because you removed the files from the index. 您无法在这些文件上设置该标志,因为您已从索引中删除了这些文件。

You clarified that the three steps you mention (create .gitignore , run rm --cached , attempt to set index flags) were done on the local clone. 您已阐明,您提到的三个步骤(创建.gitignore ,运行rm --cached ,尝试设置索引标志)是在本地克隆上完成的。 So the next question would be how to sync this up with the server, without deleting the server's content directory. 因此,下一个问题将是如何将此同步到服务器,而不删除服务器的content目录。

The index, like the worktree, is local. 像工作树一样,索引是本地的。 So the server isn't yet aware that you've done anything. 因此服务器尚未意识到您已完成任何操作。 The next time you do a commit and push it, you'll need to take steps to properly update the server (or else the content directory will be lost). 下次执行提交并将其推送时,您需要采取步骤来正确更新服务器(否则内容目录将丢失)。 I recommend doing this now, so that you don't have additional changes to juggle at the same time (and so that you don't forget to do the extra steps). 我建议立即执行此操作,以免您同时进行其他更改(并且不要忘记执行额外的步骤)。

You'll need to jump back and forth between the local and the server, so I'd go ahead and open a shell on each. 您需要在本地服务器和服务器之间来回跳转,所以我继续在每个服务器上打开一个外壳。

On the server , you need to prevent the worktree from being automatically updated by the upcoming push. 在服务器上 ,您需要防止即将来临的推送自动更新工作树。 Because your server-side repo is bare and the update occurs by way of a hook, disable the hook. 因为您的服务器端存储库是裸露的,并且更新是通过挂钩进行的,所以请禁用挂钩。 (Either move the post-receive script out of the hooks directory, or edit it and comment out the lines that checkout the changes.) (将接收后脚本移出hooks目录,或者对其进行编辑并注释掉签出更改的行。)

Now locally you need to stage the .gitignore file and commit the changes (which will include the removal (from the index) of the content directory), and push: 现在, 当地需要阶段.gitignore文件并提交更改(其中将包括的去除(从索引) content目录),并推:

git add .gitignore
git commit
git push

Now on the server you need to sync up the work tree, and re-enable the hook. 现在在服务器上,您需要同步工作树,然后重新启用挂钩。 Since the only change (other than suppressing the content folder) was the .gitignore file 由于唯一的更改(除了禁止显示content文件夹)是.gitignore文件

git checkout --work-tree /path/to/webroot -- .gitignore

will sync things back up. 将同步备份。 Then undo whatever you did to disable the hook. 然后撤消禁用挂钩的所有操作。

Just to clear concept of gitignore first, 首先要明确gitignore的概念,

  • .gitignore file says i will not detect these files and folder as a part of version controll system. .gitignore文件说,我不会将这些文件和文件夹检测为版本控制系统的一部分。
  • If file is already part of version control system and if you try to ignore then it wont be ignored. 如果文件已经是版本控制系统的一部分,并且如果您尝试忽略它,那么它将不会被忽略。
  • You can not ignore file which are part of remote repostiory. 您不能忽略属于远程邮局的文件。 Only when you delete it from remote repo and add the folder in gitignore then it will be ignored. 仅当您从远程存储库中删除它并在gitignore中添加该文件夹时,它才会被忽略。 Note : YOu wont have content/ on remote repo in this case. 注意:这种情况下,您在远程存储库上将没有内容/。

Best Solution : 最佳解决方案:

Let it be on remote repository and you do not commit the files of this folder. 使其位于远程存储库上,并且您不提交此文件夹的文件。

Hope it adds value to your question 希望它为您的问题增加价值

You shouldn't use git as a deployment tool. 您不应该将git用作部署工具。

What you could do: have a remote repo without the content folder. 您可以做什么:在没有内容文件夹的情况下进行远程回购。 Add content folder to .gitignore. 将内容文件夹添加到.gitignore。 Have your live server pull from that remote repo. 让您的实时服务器从该远程存储库中提取。 This will not change the content of your content folder. 这不会更改内容文件夹的内容。

Alternative: have your content somwhere outside your git-folder. 替代方案:将您的内容放在git文件夹之外。

Edit : To solve you problem: do the following on the remote : 编辑 :为解决您的问题:在遥控器上执行以下操作:

git rm -r --cached content/*
git commit -m "content deleted"

then backup you local content folder and git pull . 然后备份您的本地内容文件夹和git pull

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

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