【问题标题】:Folder has disappeared after gitignoregitignore 后文件夹消失了
【发布时间】:2018-10-10 19:59:18
【问题描述】:

我和我的队友正在使用 Visual Studio 在 MVC C# 项目中工作。今天我尝试做一个 .gitignore 来忽略要提交到我们存储库的文件夹,并且一个文件夹(App_Data/)已经消失(在我们的存储库中)。 我将逐步解释我所做的:

  1. 我制作了我的 .gitignore:
Tp_Integrador/.vs/
Tp_Integrador/Tp_Integrador/obj/
Tp_Integrador/packages/
Tp_Integrador/Tp_Integrador/App_Data/
Tp_Integrador/Tp_Integrador/bin/
Tp_Integrador/Tp_Integrador/Content/
Tp_Integrador/Tp_Integrador/Properties/
Tp_Integrador/Tp_Integrador/Scripts/
  1. git add .git commit -m "asdfad"git push
  2. git rm -r --cached .git add .git commit -m "Cached"
  3. 最后,在我们的存储库中,我看不到我们的文件夹 App_Data/,在 VisualStudio 中,我看到 App_Data/ 为空,一些 .cs(C# 类)无法跟踪,因为队友进行了更改,而我没有看到它们.

【问题讨论】:

  • 除了Tp_Integrador/Tp_Integrador/bin/ 文件夹之外,App_Data 中还有其他内容吗?
  • 我认为正在发生的事情是对于没有被它跟踪的文件的 git 目录,它只是“删除”它们(至少就保存在 git 数据库中的树对象而言)。
  • @maccettura 它们是不同的文件夹。每对文件夹之间有一个空格:(
  • 那么我真的很困惑,如果它们是两个不同的项目而不是Tp_Integrador/Tp_Integrador/App_Data/ 是它没有出现的原因。你真的无视它
  • 是的,但是已经从仓库中删除了

标签: c# asp.net-mvc git visual-studio github


【解决方案1】:

Git 不存储空文件夹,所以如果你想在那里有 App_Data 文件夹,你必须再次手动创建它(如果你删除了它)并在里面放一个空文件,Git 可以跟踪和确保始终创建文件夹。

一种常见的模式是在您始终希望 Git 保留的文件夹中创建一个名为 .gitkeep 的空文件,然后在您的 .gitignore 中,您告诉 Git 忽略您的文件夹,好吧,但不要忽略名为 .gitkeep(任何位置)的文件。

例如

Tp_Integrador/Tp_Integrador/App_Data/
!.gitkeep

【讨论】:

    【解决方案2】:

    当我们更改代码(例如,C# 类)时,App_Data 内容会发生变化。我想要的是及时返回并将文件夹保留为昨天

    如果可以,请将您的分支重置为问题中描述的序列之前的提交:执行git log,然后选择带有“缓存”作为注释的提交之前的提交。

    git checkout -b newBranch <oldCommit>
    git push -u origin newBranch
    

    目标是在替换旧的之前将您的更改报告给newBranch

    然后,如果您需要忽略 Tp_Integrador/Tp_Integrador 的某些部分,而不是子文件夹,请在您的 .gitignore(在 newBranch)中执行:

    Tp_Integrador/Tp_Integrador/**
    !Tp_Integrador/Tp_Integrador/**/
    !Tp_Integrador/Tp_Integrador/App_data/
    

    这样,git rm -r --cached . , git add . 将添加来自Tp_Integrador/Tp_Integrador/App_data/ 的文件,而不是来自Tp_Integrador/Tp_Integrador/ 的任何其他子文件夹的文件。

    【讨论】:

      【解决方案3】:

      最后,我重置了我的分支并从最后一个状态开始创建了一个新的存储库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 2017-12-05
        • 2021-11-09
        • 2018-07-31
        • 2013-02-10
        • 2016-12-25
        • 1970-01-01
        相关资源
        最近更新 更多