How can I add an empty directory to a Git repository?

后端 未结 30 2622
离开以前
离开以前 2020-11-21 04:52

How can I add an empty directory (that contains no files) to a Git repository?

30条回答
  •  后悔当初
    2020-11-21 05:14

    Here is a hack, but it's funny that it works (Git 2.2.1). Similar to what @Teka suggested, but easier to remember:

    • Add a submodule to any repository (git submodule add path_to_repo)
    • This will add a folder and a file .submodules. Commit a change.
    • Delete .submodules file and commit the change.

    Now, you have a directory that gets created when commit is checked out. An interesting thing though is that if you look at the content of tree object of this file you'll get:

    fatal: Not a valid object name b64338b90b4209263b50244d18278c0999867193

    I wouldn't encourage to use it though since it may stop working in the future versions of Git. Which may leave your repository corrupted.

提交回复
热议问题