【问题标题】:How can I add a blank directory to a Git repository?如何将空白目录添加到 Git 存储库?
【发布时间】:2010-09-12 01:45:21
【问题描述】:

如何将空白目录(不包含文件)添加到 Git 存储库?

【问题讨论】:

  • 虽然没用,there is a way to hack an empty (really empty) directory into your repo。但是,当前版本的 Git 不会 checkout
  • @tiwo 我不同意它没有用。你的目录层次结构是你项目的一部分,所以它应该是版本控制的。
  • 就我而言,我想为 tmp 文件添加目录结构,但不是 tmp 文件本身。通过这样做,我的测试仪具有正确的结构(否则会出现错误),但我不会用 tmp 数据阻塞我的提交。所以是的,它对我有用!
  • @AdamMarshall 我认为 tiwo 是说黑客没有用,因为它被结帐忽略了。 Tmp 目录听起来确实是 VCS 的一个有用功能。
  • 为什么不让创建tmp文件的程序也创建tmp目录呢?

标签: git directory git-add


【解决方案1】:

要将 Jamie Flournoy 的 solution 扩展为目录树,您可以将此 .gitignore 放在顶级目录中,并将 touch .keepdir 放在 git 应该跟踪的每个子目录中。所有其他文件都被忽略。这对于确保构建目录的结构一致很有用。

# Ignore files but not directories. * matches both files and directories
# but */ matches only directories. Both match at every directory level
# at or below this one.
*
!*/

# Git doesn't track empty directories, so track .keepdir files, which also 
# tracks the containing directory. 
!.keepdir

# Keep this file and the explanation of how this works
!.gitignore
!Readme.md

【讨论】:

    【解决方案2】:

    阅读@ofavre@stanislav-bashkyrtsev 的答案,使用损坏的 GIT 子模块引用来创建 GIT 目录,我很惊讶没有人建议对这个想法进行简单的修改,以使整个事情变得理智和安全:

    与其将虚假子模块侵入 GIT,不如添加一个空的真实子模块

    输入:https://gitlab.com/empty-repo/empty.git

    只有一个提交的 GIT 存储库:

    commit e84d7b81f0033399e325b8037ed2b801a5c994e0
    Author: Nobody <none>
    Date: Thu Jan 1 00:00:00 1970 +0000
    

    没有消息,没有提交的文件。

    用法

    向你的 GIT 仓库添加一个空目录:

    git submodule add https://gitlab.com/empty-repo/empty.git path/to/dir
    

    将所有现有的空目录转换为子模块:

    find . -type d -empty -delete -exec git submodule add -f https://gitlab.com/empty-repo/empty.git \{\} \;
    

    Git 将在创建子模块引用时存储最新的提交哈希,因此您不必担心我(或 GitLab)会使用它来注入恶意文件。不幸的是,我没有找到任何方法来强制在结帐期间使用哪个提交 ID,因此您必须在添加存储库后使用 git submodule status 手动检查参考提交 ID 是否为 e84d7b81f0033399e325b8037ed2b801a5c994e0

    仍然不是原生解决方案,但我们可能拥有最好的解决方案,而无需有人真的真的在 GIT 代码库中弄脏了。

    附录:重新创建此提交

    您应该能够使用(在一个空目录中)重新创建这个确切的提交:

    # Initialize new GIT repository
    git init
    
    # Set author data (don't set it as part of the `git commit` command or your default data will be stored as “commit author”)
    git config --local user.name "Nobody"
    git config --local user.email "none"
    
    # Set both the commit and the author date to the start of the Unix epoch (this cannot be done using `git commit` directly)
    export GIT_AUTHOR_DATE="Thu Jan 1 00:00:00 1970 +0000"
    export GIT_COMMITTER_DATE="Thu Jan 1 00:00:00 1970 +0000"
    
    # Add root commit
    git commit --allow-empty --allow-empty-message --no-edit
    

    创建可重现的 GIT 提交非常困难……

    【讨论】:

      【解决方案3】:

      我搜索这个问题是因为:我创建了一个新目录,其中包含许多文件。在这些文件中,有些我想添加到 git 存储库中,有些不。但是当我做“git status”时。它只显示:

      Untracked files:
        (use "git add <file>..." to include in what will be committed)
          ../trine/device_android/
      

      它没有列出这个新目录中的单独文件。然后我想也许我可以只添加这个目录,然后处理单独的文件。所以我用谷歌搜索“git add directory only”。

      在我的情况下,我发现我可以在新目录中添加一个文件,我确定我想将它添加到 git。

      git add new_folder/some_file
      

      在此之后,“git status”将显示单独文件的状态。

      【讨论】:

        【解决方案4】:

        这个解决方案对我有用。

        1。将.gitignore 文件添加到您的空目录:

        *
        */
        !.gitignore
        
        • *忽略文件夹中的所有文件
        • */忽略子目录
        • !.gitignore 包含 .gitignore 文件

        2。然后删除缓存、暂存文件、提交和推送:

        git rm -r --cached .
        git add . // or git stage .
        git commit -m ".gitignore fix"
        git push
        

        【讨论】:

        • 这很棒。我曾经在主 .gitignore 中执行 .gitkeep 文件和类似的几行,但我更喜欢这个。只是看起来更像 git-y。谢谢。
        【解决方案5】:

        只需添加 readme.gitignore 然后删除它,而不是从终端,从 github 网站,这将提供一个空的存储库

        【讨论】:

        • 他们要求的是一个空目录,而不是一个空的存储库。另外,当他们只谈论 git 时,你就假设他们使用的是 GitHub。
        【解决方案6】:

        只需在要跟踪的空目录中添加空(无内容).gitignore 文件即可。

        例如如果你想跟踪空目录/project/content/posts 然后创建新的空文件/project/content/posts/.gitignore

        注意:.gitkeep 不是官方git 的一部分:

        【讨论】:

          【解决方案7】:

          在该目录中添加一个 .gitkeep 文件并提交它。

          touch .gitkeep 
          

          它是git遵循的标准。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-01-07
            • 1970-01-01
            • 1970-01-01
            • 2019-03-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多