How to track directories but not their files with Git?

I’ve recently started using Git and am having trouble with just one thing. How can I track directories without tracking their contents?

For example the site I’m working on allows uploads. I want to track the uploads directory so that it is created when branching, etc. but obviously not the files within it (test files whilst in develop branch or the real files in master).

  • In BASH How Can One Recursively Copy Only Content Version-Controlled by Git?
  • How to make SVN ADD ignore binaries
  • How to best organize a directory structure of a dictionary?
  • Use forever-branches or directories for project related documentation, wireframes, designs, etc.?
  • Git add a folder with spaces in the name
  • Git repository within Git repository
  • In my .gitignore I have the following:

    uploads/*.*

    Have also tried (which ignores the whole directory):

    uploads/

    This directory may also contain sub directories (uploads/thumbs/ uploads/videos/) I would like to be able to track these but not their files.

    Is this possible with Git? I’ve searched everywhere without finding an answer.

  • What is the correct way to configure Qt Creator to use Git?
  • Pulling from Git to Wordpress and Desktop Server
  • Replace visual studio 2012 Diff tool with winmerge when using Microsoft Git Provider
  • git buggy modified files when changing branches
  • How does git track changes to files
  • How to setup current branch (dev) to push to refs/remotes/test with git push only?
  • 4 Solutions collect form web for “How to track directories but not their files with Git?”

    Git doesn’t track directories, it tracks files, so to acheive this you need to track at least one file. So assuming your .gitignore file looks something like this:

    upload/*
    

    You can do this:

    $ touch upload/.placeholder
    $ git add -f upload/.placeholder
    

    If you forget the -f you’ll see:

    $ git add upload/.placeholder
    The following paths are ignored by one of your .gitignore files:
    upload
    Use -f if you really want to add them.
    fatal: no files added
    

    Then when you do git status you’ll see:

    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached ..." to unstage)
    #
    #   new file:   upload/.placeholder
    #
    

    Obviously you can then do:

    $ touch upload/images/.placeholder
    $ git add -f upload/images/.placeholder
    

    I wrote about this here.

    Add a .gitignore within the directory.

    Best answerd I’ve found is to include a .gitignore file in your upload folder with this content

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

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

    The best solution so far:

    1) Create a .gitignore file

    2) Write inside:

    *
    */
    !.gitignore
    

    3) Add the .gitignore file to the folder that you want.

    Source: https://stackoverflow.com/a/5581995/2958543

    Git Baby is a git and github fan, let's start git clone.