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

后端 未结 30 2460
离开以前
离开以前 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:07

    You could always put a README file in the directory with an explanation of why you want this, otherwise empty, directory in the repository.

    0 讨论(0)
  • 2020-11-21 05:07

    As mentioned it's not possible to add empty directories, but here is a one liner that adds empty .gitignore files to all directories.

    ruby -e 'require "fileutils" ; Dir.glob(["target_directory","target_directory/**"]).each { |f| FileUtils.touch(File.join(f, ".gitignore")) if File.directory?(f) }'

    I have stuck this in a Rakefile for easy access.

    0 讨论(0)
  • 2020-11-21 05:08

    Many have already answered this question. Just adding a PowerShell version here.

    Find all the empty folders in the directory

    Add a empty .gitkeep file in there

    Get-ChildItem 'Path to your Folder' -Recurse -Directory | Where-Object {[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -eq 0} | ForEach-Object { New-Item ($_.FullName + "\.gitkeep") -ItemType file}
    
    0 讨论(0)
  • 2020-11-21 05:10

    Let's say you need an empty directory named tmp :

    $ mkdir tmp
    $ touch tmp/.gitignore
    $ git add tmp
    $ echo '*' > tmp/.gitignore
    $ git commit -m 'Empty directory' tmp
    

    In other words, you need to add the .gitignore file to the index before you can tell Git to ignore it (and everything else in the empty directory).

    0 讨论(0)
  • 2020-11-21 05:10

    The solution of Jamie Flournoy works great. Here is a bit enhanced version to keep the .htaccess :

    # Ignore everything in this directory
    *
    # Except this file
    !.gitignore
    !.htaccess
    

    With this solution you are able to commit a empty folder, for example /log, /tmp or /cache and the folder will stay empty.

    0 讨论(0)
  • 2020-11-21 05:11

    Create an empty file called .gitkeep in the directory, and add that.

    0 讨论(0)
提交回复
热议问题