How to track directories but not their files with Git?

git not tracking empty directory
git keep folder, but ignore contents
force git to create empty directory
gitkeep
git repo add empty folder
git not recognizing new folder
force git to add empty directory
git create folder

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 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.

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

How can I git add a folder but none of its contents?, You probably do not want to add the actual log files into your git, but adding the directory could make sense, to avoid warning such like this: There are times when you'd like to track an empty directory within git but there's a problem: git wont allow you to add a directory that doesn't have a file in it. The easy solution is putting an empty stub file within the directory, and the industry standard for that stub file name is.gitkeep.

I wrote about this here.

Add a .gitignore within the directory.

How to track a directory but not it's files with git (Example), tl;dr create a .keep file to check-in folders in git and add the following lines I find it very useful and I want to share it. Now, if you also want be able to check-in the folder, but not its content — makes sense for a logs file, add  Untrack files in Git Last updated: 17 Jun 2020 As far as I know, there are at least 2 ways to untrack a file using git:. Remove file from the repository but keep it in your working 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?

Hey git, please .keep those folders - Kinduff, There are times when you'd like to track an empty directory within git but there's the directory, and the industry standard for that stub file name is .gitkeep . It's no secret that Facebook has become a major traffic driver for all  git update-index --assume-unchanged <file> To resume change tracking: git update-index --no-assume-unchanged <file> Permanently ignore changes to a file. If a file is already tracked by Git, adding that file to your .gitignore isn't enough to ignore changes to the file. You also need to remove the information about the file from Git's index.

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

Track Empty Directories with git, keep is not a "magical" name but rather a popular convention. Also, the file doesn​'t serve any other purpose beyond making its containing folder visible to Git. Tip. All other files in the working directory are untracked and git is not aware of those files. Sometimes your git working directory may get cluttered up with unnecessary files that are either auto-generated, leftover from merges or created by mistake.

In order to track only directories but not the files, I did the following. Thanks to the @PeterFarmer's comment on git tracking only files, I've been able to keep all directories excluding the files as described in below.

# exclude everything in every folder
/data/**/*.*

# include only .gitkeep files
!/data/**/*.gitkeep

Adding this to the .gitignore file will do the work. The following is my folder structure.

data/
├── processed
│   ├── dataset1.csv
│   └── dataset2.csv
├── raw
│   ├── raw_dataset1.json
└── test
    ├── subfolder
    │   └── dataset2.csv
    └── reviews.csv

When I do git add . && git status, git only recognizes folders, but not files.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore
        new file:   data/processed/.gitkeep
        new file:   data/raw/.gitkeep
        new file:   data/test/.gitkeep
        new file:   data/test/subfolder/.gitkeep

Keep in mind that the following for your .gitignore files:

Prepending slash looks only for the root directory.

/dir

Double asterisk looks for zero or more directories.

/**/

How can I add an empty folder to version control in Git?, Git doesn't track directories; but just the files within them. of your .gitignore files: my-dir/index.html Use -f if you really want to add them. fatal: no files added. There's a rule in one of your gitignores that prevent you from adding that directory​. For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write: # exclude everything somefolder/* # exception to the rule !somefolder/.gitkeep Commit your .gitignore and .gitkeep files and this should resolve your issue.

can't add folder in git, But there are times when having the folder in the repo would be ever contain files considered to be “content”—that is, they are not files that I  The only way I could actually stop git form tracking the folder was to do the following: Make a backup of the local folder and put in a safe place. Delete the folder from your local repo. Make sure cache is cleared git rm -r --cached your_folder/. Add your_folder/ to .gitignore.

Add an empty directory to a Git repository, Git only "tracks" a directory when there are files within the directory that it's tracking. Add the files in a directory and the directory itself is added by implication​. git commit -am "msg" is not same as git add file and git commit -m "msg" If you have some files which were never added to git tracking you still need to do git add file. The “git commit -a” command is a shortcut to a two-step process. After you modify a file that is already known by the repo, you still have to tell the repo, “Hey!

Folder not being tracked : git, Untracked files are everything else — any files in your working directory that were not In order to begin tracking a new file, you use the command git add . to unstage) new file: README Changes not staged for commit: (use "git add <file>. Tracking new and modified files. To start tracking a single modified file, type the following in the terminal window as: git add filename. To track all the files in the project directory at a single instance, particularly when starting with a new project, type the following: git add . So now we can say that all the new as well as the modified

Comments
  • Git only tracks files. What you could do is play around with git hooks and track a file that will contain the directory listing (auto-generated using the hooks). Although I'm not entirely sure if that would be possible.
  • More concrete: Git tracks content and (for git) an empty directory is not content. You can argue, wether you agree, or not, but as another "workaround" you may think about creating the directories on build/install/when-needed. However: I use the ".placeholder-file"-solution also ;)
  • Rather than use .placeholder, I use a .gitignore file. Not only does this take care of the exclusions within the directory, but it acts as a placeholder itself.
  • Thanks, this is just what I need.
  • A common convention for a placeholder is a gitignore file.
  • @Jefromi Indeed, as my comments to Abizern, had never considered using the .gitignore file as the placeholder.
  • Oops, somehow missed those comments.
  • Git doesn't track directories, it tracks files sounds funny when they teach you that everything is a file in UNIX, even a directory is a file
  • Nice little article and comments! Had never considered using .gitignore as the placeholder file....
  • Thanks. I learned something just by writing a post and getting comments.
  • There seems to be a popular naming convention now to use .gitkeep
  • just got that "ha-ha" mooment right now, i wish i could upvote 6 different times
  • This is the most elegant solution
  • Thank you! I'm using this way and it is very helpful