Does Git delete empty folders?How to setup git for RStudio projects on shared Windows foldersgit pull does...

Is a butterfly one or two animals?

To "hit home" in German

Why did this happen to Thanos's ships at the end of "Avengers: Endgame"?

How can I use unicode in this condition?

In an emergency, how do I find and share my position?

Can pay be witheld for hours cleaning up after closing time?

How to "know" if I have a passion?

Does Git delete empty folders?

Is it safe to remove the bottom chords of a series of garage roof trusses?

Why we don't have vaccination against all diseases which are caused by microbes?

How to specify and fit a hybrid machine learning - linear model

Thread-safe, Convenient and Performant Random Number Generator

Why were movies shot on film shot at 24 frames per second?

How to compare two different formulations of a problem?

How much code would a codegolf golf if a codegolf could golf code?

Do I have to learn /o/ or /ɔ/ separately?

Is "stainless" a bulk or a surface property of stainless steel?

Are required indicators necessary for radio buttons?

Vacuum collapse -- why do strong metals implode but glass doesn't?

How would one country purchase another?

How to analyze "dearly beloved"?

What is the difference between a premise and an assumption in logic?

Can we save the word "unique"?

Overwrite file only if data



Does Git delete empty folders?


How to setup git for RStudio projects on shared Windows foldersgit pull does nothing / git push just hangs / debug1: expecting SSH2_MSG_KEX_ECDH_REPLYEclipse IDE Git client pushing code to github and getting “deletion of the current branch prohibited”why does ssh to github require the git user?Why did deleting .git delete all tracked files?Tortoise Git Commit LostBring a local folder to remote git repo






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







36















I deleted all files from a directory in my work folder, but the directory itself still exists. After pushing the changes to a remote repository (GitHub) I checked my project and noticed that the directory was gone. Is it possible that Git deletes empty folders?










share|improve this question









New contributor



vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 15





    Git does not delete empty folder but it has no concept of folders therefore it does not track folders (git only tracks paths to your files, not folders, it treats file paths like how you'd treat URLs on a website). The standard way to keep an empty folder in your project is to add a README file so git can track that file instead.

    – slebetman
    2 days ago








  • 36





    @slebetman Actually, as mentioned in my answer, the commonly accepted way to keep an empty directory is to add a .gitkeep file, not a README, as a .gitkeep file will usually not be listed (due to starting with a .) when browsing directories. .gitkeep files are therefore meant to be (almost) invisible. A README on the other hand by its naming implies some meaning – it almost tells you that there is something that should be in there, but it's not.

    – slhck
    2 days ago






  • 4





    @slebetman I understand. It's a matter of preference, I suppose. I see both ways listed here, but personally I've most come across the former.

    – slhck
    2 days ago






  • 17





    @slebetman - you don't "open" a .gitkeep file, it's a zero byte file used only to tell git to track the folder. And it's a de facto standard, so there is no reason to ever check why it was added. It's only ever added to keep the folder, that's why it's called gitKEEP.

    – Davor
    yesterday






  • 5





    @slebetman - uh, yeah, README is standard, but for a completely different thing. It has nothing to do with preserving empty directories, which is the only reason why gitkeep would ever exist. Using a wrong thing is exactly what would confuse people.

    – Davor
    18 hours ago




















36















I deleted all files from a directory in my work folder, but the directory itself still exists. After pushing the changes to a remote repository (GitHub) I checked my project and noticed that the directory was gone. Is it possible that Git deletes empty folders?










share|improve this question









New contributor



vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 15





    Git does not delete empty folder but it has no concept of folders therefore it does not track folders (git only tracks paths to your files, not folders, it treats file paths like how you'd treat URLs on a website). The standard way to keep an empty folder in your project is to add a README file so git can track that file instead.

    – slebetman
    2 days ago








  • 36





    @slebetman Actually, as mentioned in my answer, the commonly accepted way to keep an empty directory is to add a .gitkeep file, not a README, as a .gitkeep file will usually not be listed (due to starting with a .) when browsing directories. .gitkeep files are therefore meant to be (almost) invisible. A README on the other hand by its naming implies some meaning – it almost tells you that there is something that should be in there, but it's not.

    – slhck
    2 days ago






  • 4





    @slebetman I understand. It's a matter of preference, I suppose. I see both ways listed here, but personally I've most come across the former.

    – slhck
    2 days ago






  • 17





    @slebetman - you don't "open" a .gitkeep file, it's a zero byte file used only to tell git to track the folder. And it's a de facto standard, so there is no reason to ever check why it was added. It's only ever added to keep the folder, that's why it's called gitKEEP.

    – Davor
    yesterday






  • 5





    @slebetman - uh, yeah, README is standard, but for a completely different thing. It has nothing to do with preserving empty directories, which is the only reason why gitkeep would ever exist. Using a wrong thing is exactly what would confuse people.

    – Davor
    18 hours ago
















36












36








36


2






I deleted all files from a directory in my work folder, but the directory itself still exists. After pushing the changes to a remote repository (GitHub) I checked my project and noticed that the directory was gone. Is it possible that Git deletes empty folders?










share|improve this question









New contributor



vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I deleted all files from a directory in my work folder, but the directory itself still exists. After pushing the changes to a remote repository (GitHub) I checked my project and noticed that the directory was gone. Is it possible that Git deletes empty folders?







git github






share|improve this question









New contributor



vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 17 hours ago









Peter Mortensen

8,52416 gold badges62 silver badges85 bronze badges




8,52416 gold badges62 silver badges85 bronze badges






New contributor



vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 2 days ago









vitaliy4usvitaliy4us

1842 silver badges3 bronze badges




1842 silver badges3 bronze badges




New contributor



vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




vitaliy4us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • 15





    Git does not delete empty folder but it has no concept of folders therefore it does not track folders (git only tracks paths to your files, not folders, it treats file paths like how you'd treat URLs on a website). The standard way to keep an empty folder in your project is to add a README file so git can track that file instead.

    – slebetman
    2 days ago








  • 36





    @slebetman Actually, as mentioned in my answer, the commonly accepted way to keep an empty directory is to add a .gitkeep file, not a README, as a .gitkeep file will usually not be listed (due to starting with a .) when browsing directories. .gitkeep files are therefore meant to be (almost) invisible. A README on the other hand by its naming implies some meaning – it almost tells you that there is something that should be in there, but it's not.

    – slhck
    2 days ago






  • 4





    @slebetman I understand. It's a matter of preference, I suppose. I see both ways listed here, but personally I've most come across the former.

    – slhck
    2 days ago






  • 17





    @slebetman - you don't "open" a .gitkeep file, it's a zero byte file used only to tell git to track the folder. And it's a de facto standard, so there is no reason to ever check why it was added. It's only ever added to keep the folder, that's why it's called gitKEEP.

    – Davor
    yesterday






  • 5





    @slebetman - uh, yeah, README is standard, but for a completely different thing. It has nothing to do with preserving empty directories, which is the only reason why gitkeep would ever exist. Using a wrong thing is exactly what would confuse people.

    – Davor
    18 hours ago
















  • 15





    Git does not delete empty folder but it has no concept of folders therefore it does not track folders (git only tracks paths to your files, not folders, it treats file paths like how you'd treat URLs on a website). The standard way to keep an empty folder in your project is to add a README file so git can track that file instead.

    – slebetman
    2 days ago








  • 36





    @slebetman Actually, as mentioned in my answer, the commonly accepted way to keep an empty directory is to add a .gitkeep file, not a README, as a .gitkeep file will usually not be listed (due to starting with a .) when browsing directories. .gitkeep files are therefore meant to be (almost) invisible. A README on the other hand by its naming implies some meaning – it almost tells you that there is something that should be in there, but it's not.

    – slhck
    2 days ago






  • 4





    @slebetman I understand. It's a matter of preference, I suppose. I see both ways listed here, but personally I've most come across the former.

    – slhck
    2 days ago






  • 17





    @slebetman - you don't "open" a .gitkeep file, it's a zero byte file used only to tell git to track the folder. And it's a de facto standard, so there is no reason to ever check why it was added. It's only ever added to keep the folder, that's why it's called gitKEEP.

    – Davor
    yesterday






  • 5





    @slebetman - uh, yeah, README is standard, but for a completely different thing. It has nothing to do with preserving empty directories, which is the only reason why gitkeep would ever exist. Using a wrong thing is exactly what would confuse people.

    – Davor
    18 hours ago










15




15





Git does not delete empty folder but it has no concept of folders therefore it does not track folders (git only tracks paths to your files, not folders, it treats file paths like how you'd treat URLs on a website). The standard way to keep an empty folder in your project is to add a README file so git can track that file instead.

– slebetman
2 days ago







Git does not delete empty folder but it has no concept of folders therefore it does not track folders (git only tracks paths to your files, not folders, it treats file paths like how you'd treat URLs on a website). The standard way to keep an empty folder in your project is to add a README file so git can track that file instead.

– slebetman
2 days ago






36




36





@slebetman Actually, as mentioned in my answer, the commonly accepted way to keep an empty directory is to add a .gitkeep file, not a README, as a .gitkeep file will usually not be listed (due to starting with a .) when browsing directories. .gitkeep files are therefore meant to be (almost) invisible. A README on the other hand by its naming implies some meaning – it almost tells you that there is something that should be in there, but it's not.

– slhck
2 days ago





@slebetman Actually, as mentioned in my answer, the commonly accepted way to keep an empty directory is to add a .gitkeep file, not a README, as a .gitkeep file will usually not be listed (due to starting with a .) when browsing directories. .gitkeep files are therefore meant to be (almost) invisible. A README on the other hand by its naming implies some meaning – it almost tells you that there is something that should be in there, but it's not.

– slhck
2 days ago




4




4





@slebetman I understand. It's a matter of preference, I suppose. I see both ways listed here, but personally I've most come across the former.

– slhck
2 days ago





@slebetman I understand. It's a matter of preference, I suppose. I see both ways listed here, but personally I've most come across the former.

– slhck
2 days ago




17




17





@slebetman - you don't "open" a .gitkeep file, it's a zero byte file used only to tell git to track the folder. And it's a de facto standard, so there is no reason to ever check why it was added. It's only ever added to keep the folder, that's why it's called gitKEEP.

– Davor
yesterday





@slebetman - you don't "open" a .gitkeep file, it's a zero byte file used only to tell git to track the folder. And it's a de facto standard, so there is no reason to ever check why it was added. It's only ever added to keep the folder, that's why it's called gitKEEP.

– Davor
yesterday




5




5





@slebetman - uh, yeah, README is standard, but for a completely different thing. It has nothing to do with preserving empty directories, which is the only reason why gitkeep would ever exist. Using a wrong thing is exactly what would confuse people.

– Davor
18 hours ago







@slebetman - uh, yeah, README is standard, but for a completely different thing. It has nothing to do with preserving empty directories, which is the only reason why gitkeep would ever exist. Using a wrong thing is exactly what would confuse people.

– Davor
18 hours ago












4 Answers
4






active

oldest

votes


















78














Why is the directory not shown?



Git does not track directories; it only tracks files.



If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via git add if it's empty.



In other words: If you can see the directory locally in your file browser, but it disappeared from GitHub, you most likely removed all files from the directory, added that change to the index, and committed and pushed it.



How do I track an empty directory, then?



If you want to explicitly track an empty directory, you have to create a file in it. Since Git won't track empty directories, you have to trick it into doing so by adding a file in the directory to Git's index.



Usually, people store a file called .gitkeep in a directory that they wish to track, but where the directory should stay empty for the time being. You can give the file any other name, of course, but the name .gitkeep is a convention. The .gitkeep file (due to starting with a .) will not be shown by file listings on most systems.



Instead of .gitkeep, some users also like to put a README file there instead, ideally with a short description of why the directory has to exist in the first place.



Example



$ mkdir foo
$ git init
$ git add .
$ git ls-files # which files does Git know about?
# apparently, none
$ touch foo/bar # create a file in the directory
$ git add .
$ git ls-files # does Git know about it now?
foo/bar # yep!


Here, the foo directory only gets added to the index once a file is in it.



What if I really want to track an empty directory?



That all said, in principle, the underlying data structure allows Git to store an empty directory, since it would be represented by an empty “tree”. Some further reading here and here.






share|improve this answer























  • 9





    cannot is somewhat misleading. Structure of object files (trees and blobs) supports empty directories. It just deliberately decided not to track empty directories

    – RiaD
    2 days ago











  • @RiaD Good point. I updated my answer to be a bit more precise.

    – slhck
    yesterday











  • Git does not even track files, it tracks file contents. That's why it can recognize renamed files on the fly.

    – Davor
    16 hours ago






  • 2





    @Davor Well, that's a bit of a matter of definition. Yes, it tracks file content primarily, but it also tracks the paths that point to that content. The tree entries that Git stores are filenames, and they point to the actual content.

    – slhck
    15 hours ago



















5














Add the following .gitignore file to the folders you wish to include in your repo.



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


Adding .gitkeep works but isn’t an official solution. See https://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep






share|improve this answer





















  • 23





    This will indeed ensure Git tracks the empty directory, but it will also prevent Git from tracking any other files in that directory — so this is for if you want a directory that should always stay empty in Git.

    – NobodyNada
    2 days ago






  • 2





    I think @NobodyNada's comment should be part of this answer. Some people might want their Git project to contain a directory that is empty and never tracked (like a build directory?)

    – TomTsagk
    yesterday






  • 14





    There's no such thing as an "official" solution. You just can't have empty directories in git, period. Adding a file, any file to the directory works because then it's no longer an empty directory. This is perfectly documented, "official" behaviour. .gitignore is in the documentation, unlike other file names you might use, because it does unrelated stuff: make git ignore specific files. That doesn't make it a better solution to this problem. On the contrary, that makes it confusing as people will expect it to do that other stuff, and not just be there to keep the directory.

    – FrederikVds
    yesterday













  • I tried editing the answer to include NobodyNada's point, but the edit was rejected. Hopefully nobody misses the comment and loses data.

    – chicks
    17 hours ago



















2














Git does not track folders. In order to refrain from accumulating messes, when the index changes from (indirectly) referencing a directory to not referencing it (by doing something like git rm on the last registered file in the directory), Git tries removing the directory before it loses track of it by it no longer being in the index. If Git is successful because the directory has indeed become empty (and does not contain unregistered files), the directory will be gone, if not, it will stick around since Git no longer has notice of it.






share|improve this answer

































    0














    Git might delete folders if you ran git clean with -dffx (possibly even with a subset of those options).



    As @TRiG mentions in the comments, if the directory is empty when you commit, it won’t show up in, e.g., GitHub, because git doesn’t track directories (it track files)—the tree where those files were is no longer relevant because there are no files, so there is no tree.



    Without more detail, it’s practically impossible for us to properly answer your underlying questions (what happened?).






    share|improve this answer










    New contributor



    D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















    • The OP "deleted all files from a directory" and committed those changes. So I believe it's quite clear what happened.

      – slhck
      yesterday











    • @slhck not so—how did they delete? rm -rf? git clean? Drag and drop to the recycle bin? What does « checked my project » mean, and which directory was gone?

      – D. Ben Knoble
      yesterday











    • To me, it's pretty reasonable to assume that it was rm foo/* (or any other method of deleting), git add foo, then commit and push. When you check your project on GitHub (as the OP indicated), the directory will be gone.

      – slhck
      yesterday











    • @slhck i see a lot of assumptions—the deletion, as mentioned. Checking on GitHub? Where is that mentioned? I prefer not to assume I know the steps to reproduce something until I know them—ive been blinded to the true cause of bugs that way before.

      – D. Ben Knoble
      yesterday






    • 2





      Deleting all files from a directly, not running git clean -d or similar, comitting, and pushing will have the observed effect (the empty directory still exists locally, but has disappeared from the remote project).

      – TRiG
      yesterday














    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    vitaliy4us is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1472515%2fdoes-git-delete-empty-folders%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    78














    Why is the directory not shown?



    Git does not track directories; it only tracks files.



    If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via git add if it's empty.



    In other words: If you can see the directory locally in your file browser, but it disappeared from GitHub, you most likely removed all files from the directory, added that change to the index, and committed and pushed it.



    How do I track an empty directory, then?



    If you want to explicitly track an empty directory, you have to create a file in it. Since Git won't track empty directories, you have to trick it into doing so by adding a file in the directory to Git's index.



    Usually, people store a file called .gitkeep in a directory that they wish to track, but where the directory should stay empty for the time being. You can give the file any other name, of course, but the name .gitkeep is a convention. The .gitkeep file (due to starting with a .) will not be shown by file listings on most systems.



    Instead of .gitkeep, some users also like to put a README file there instead, ideally with a short description of why the directory has to exist in the first place.



    Example



    $ mkdir foo
    $ git init
    $ git add .
    $ git ls-files # which files does Git know about?
    # apparently, none
    $ touch foo/bar # create a file in the directory
    $ git add .
    $ git ls-files # does Git know about it now?
    foo/bar # yep!


    Here, the foo directory only gets added to the index once a file is in it.



    What if I really want to track an empty directory?



    That all said, in principle, the underlying data structure allows Git to store an empty directory, since it would be represented by an empty “tree”. Some further reading here and here.






    share|improve this answer























    • 9





      cannot is somewhat misleading. Structure of object files (trees and blobs) supports empty directories. It just deliberately decided not to track empty directories

      – RiaD
      2 days ago











    • @RiaD Good point. I updated my answer to be a bit more precise.

      – slhck
      yesterday











    • Git does not even track files, it tracks file contents. That's why it can recognize renamed files on the fly.

      – Davor
      16 hours ago






    • 2





      @Davor Well, that's a bit of a matter of definition. Yes, it tracks file content primarily, but it also tracks the paths that point to that content. The tree entries that Git stores are filenames, and they point to the actual content.

      – slhck
      15 hours ago
















    78














    Why is the directory not shown?



    Git does not track directories; it only tracks files.



    If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via git add if it's empty.



    In other words: If you can see the directory locally in your file browser, but it disappeared from GitHub, you most likely removed all files from the directory, added that change to the index, and committed and pushed it.



    How do I track an empty directory, then?



    If you want to explicitly track an empty directory, you have to create a file in it. Since Git won't track empty directories, you have to trick it into doing so by adding a file in the directory to Git's index.



    Usually, people store a file called .gitkeep in a directory that they wish to track, but where the directory should stay empty for the time being. You can give the file any other name, of course, but the name .gitkeep is a convention. The .gitkeep file (due to starting with a .) will not be shown by file listings on most systems.



    Instead of .gitkeep, some users also like to put a README file there instead, ideally with a short description of why the directory has to exist in the first place.



    Example



    $ mkdir foo
    $ git init
    $ git add .
    $ git ls-files # which files does Git know about?
    # apparently, none
    $ touch foo/bar # create a file in the directory
    $ git add .
    $ git ls-files # does Git know about it now?
    foo/bar # yep!


    Here, the foo directory only gets added to the index once a file is in it.



    What if I really want to track an empty directory?



    That all said, in principle, the underlying data structure allows Git to store an empty directory, since it would be represented by an empty “tree”. Some further reading here and here.






    share|improve this answer























    • 9





      cannot is somewhat misleading. Structure of object files (trees and blobs) supports empty directories. It just deliberately decided not to track empty directories

      – RiaD
      2 days ago











    • @RiaD Good point. I updated my answer to be a bit more precise.

      – slhck
      yesterday











    • Git does not even track files, it tracks file contents. That's why it can recognize renamed files on the fly.

      – Davor
      16 hours ago






    • 2





      @Davor Well, that's a bit of a matter of definition. Yes, it tracks file content primarily, but it also tracks the paths that point to that content. The tree entries that Git stores are filenames, and they point to the actual content.

      – slhck
      15 hours ago














    78












    78








    78







    Why is the directory not shown?



    Git does not track directories; it only tracks files.



    If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via git add if it's empty.



    In other words: If you can see the directory locally in your file browser, but it disappeared from GitHub, you most likely removed all files from the directory, added that change to the index, and committed and pushed it.



    How do I track an empty directory, then?



    If you want to explicitly track an empty directory, you have to create a file in it. Since Git won't track empty directories, you have to trick it into doing so by adding a file in the directory to Git's index.



    Usually, people store a file called .gitkeep in a directory that they wish to track, but where the directory should stay empty for the time being. You can give the file any other name, of course, but the name .gitkeep is a convention. The .gitkeep file (due to starting with a .) will not be shown by file listings on most systems.



    Instead of .gitkeep, some users also like to put a README file there instead, ideally with a short description of why the directory has to exist in the first place.



    Example



    $ mkdir foo
    $ git init
    $ git add .
    $ git ls-files # which files does Git know about?
    # apparently, none
    $ touch foo/bar # create a file in the directory
    $ git add .
    $ git ls-files # does Git know about it now?
    foo/bar # yep!


    Here, the foo directory only gets added to the index once a file is in it.



    What if I really want to track an empty directory?



    That all said, in principle, the underlying data structure allows Git to store an empty directory, since it would be represented by an empty “tree”. Some further reading here and here.






    share|improve this answer















    Why is the directory not shown?



    Git does not track directories; it only tracks files.



    If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via git add if it's empty.



    In other words: If you can see the directory locally in your file browser, but it disappeared from GitHub, you most likely removed all files from the directory, added that change to the index, and committed and pushed it.



    How do I track an empty directory, then?



    If you want to explicitly track an empty directory, you have to create a file in it. Since Git won't track empty directories, you have to trick it into doing so by adding a file in the directory to Git's index.



    Usually, people store a file called .gitkeep in a directory that they wish to track, but where the directory should stay empty for the time being. You can give the file any other name, of course, but the name .gitkeep is a convention. The .gitkeep file (due to starting with a .) will not be shown by file listings on most systems.



    Instead of .gitkeep, some users also like to put a README file there instead, ideally with a short description of why the directory has to exist in the first place.



    Example



    $ mkdir foo
    $ git init
    $ git add .
    $ git ls-files # which files does Git know about?
    # apparently, none
    $ touch foo/bar # create a file in the directory
    $ git add .
    $ git ls-files # does Git know about it now?
    foo/bar # yep!


    Here, the foo directory only gets added to the index once a file is in it.



    What if I really want to track an empty directory?



    That all said, in principle, the underlying data structure allows Git to store an empty directory, since it would be represented by an empty “tree”. Some further reading here and here.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered 2 days ago









    slhckslhck

    170k48 gold badges473 silver badges490 bronze badges




    170k48 gold badges473 silver badges490 bronze badges











    • 9





      cannot is somewhat misleading. Structure of object files (trees and blobs) supports empty directories. It just deliberately decided not to track empty directories

      – RiaD
      2 days ago











    • @RiaD Good point. I updated my answer to be a bit more precise.

      – slhck
      yesterday











    • Git does not even track files, it tracks file contents. That's why it can recognize renamed files on the fly.

      – Davor
      16 hours ago






    • 2





      @Davor Well, that's a bit of a matter of definition. Yes, it tracks file content primarily, but it also tracks the paths that point to that content. The tree entries that Git stores are filenames, and they point to the actual content.

      – slhck
      15 hours ago














    • 9





      cannot is somewhat misleading. Structure of object files (trees and blobs) supports empty directories. It just deliberately decided not to track empty directories

      – RiaD
      2 days ago











    • @RiaD Good point. I updated my answer to be a bit more precise.

      – slhck
      yesterday











    • Git does not even track files, it tracks file contents. That's why it can recognize renamed files on the fly.

      – Davor
      16 hours ago






    • 2





      @Davor Well, that's a bit of a matter of definition. Yes, it tracks file content primarily, but it also tracks the paths that point to that content. The tree entries that Git stores are filenames, and they point to the actual content.

      – slhck
      15 hours ago








    9




    9





    cannot is somewhat misleading. Structure of object files (trees and blobs) supports empty directories. It just deliberately decided not to track empty directories

    – RiaD
    2 days ago





    cannot is somewhat misleading. Structure of object files (trees and blobs) supports empty directories. It just deliberately decided not to track empty directories

    – RiaD
    2 days ago













    @RiaD Good point. I updated my answer to be a bit more precise.

    – slhck
    yesterday





    @RiaD Good point. I updated my answer to be a bit more precise.

    – slhck
    yesterday













    Git does not even track files, it tracks file contents. That's why it can recognize renamed files on the fly.

    – Davor
    16 hours ago





    Git does not even track files, it tracks file contents. That's why it can recognize renamed files on the fly.

    – Davor
    16 hours ago




    2




    2





    @Davor Well, that's a bit of a matter of definition. Yes, it tracks file content primarily, but it also tracks the paths that point to that content. The tree entries that Git stores are filenames, and they point to the actual content.

    – slhck
    15 hours ago





    @Davor Well, that's a bit of a matter of definition. Yes, it tracks file content primarily, but it also tracks the paths that point to that content. The tree entries that Git stores are filenames, and they point to the actual content.

    – slhck
    15 hours ago













    5














    Add the following .gitignore file to the folders you wish to include in your repo.



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


    Adding .gitkeep works but isn’t an official solution. See https://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep






    share|improve this answer





















    • 23





      This will indeed ensure Git tracks the empty directory, but it will also prevent Git from tracking any other files in that directory — so this is for if you want a directory that should always stay empty in Git.

      – NobodyNada
      2 days ago






    • 2





      I think @NobodyNada's comment should be part of this answer. Some people might want their Git project to contain a directory that is empty and never tracked (like a build directory?)

      – TomTsagk
      yesterday






    • 14





      There's no such thing as an "official" solution. You just can't have empty directories in git, period. Adding a file, any file to the directory works because then it's no longer an empty directory. This is perfectly documented, "official" behaviour. .gitignore is in the documentation, unlike other file names you might use, because it does unrelated stuff: make git ignore specific files. That doesn't make it a better solution to this problem. On the contrary, that makes it confusing as people will expect it to do that other stuff, and not just be there to keep the directory.

      – FrederikVds
      yesterday













    • I tried editing the answer to include NobodyNada's point, but the edit was rejected. Hopefully nobody misses the comment and loses data.

      – chicks
      17 hours ago
















    5














    Add the following .gitignore file to the folders you wish to include in your repo.



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


    Adding .gitkeep works but isn’t an official solution. See https://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep






    share|improve this answer





















    • 23





      This will indeed ensure Git tracks the empty directory, but it will also prevent Git from tracking any other files in that directory — so this is for if you want a directory that should always stay empty in Git.

      – NobodyNada
      2 days ago






    • 2





      I think @NobodyNada's comment should be part of this answer. Some people might want their Git project to contain a directory that is empty and never tracked (like a build directory?)

      – TomTsagk
      yesterday






    • 14





      There's no such thing as an "official" solution. You just can't have empty directories in git, period. Adding a file, any file to the directory works because then it's no longer an empty directory. This is perfectly documented, "official" behaviour. .gitignore is in the documentation, unlike other file names you might use, because it does unrelated stuff: make git ignore specific files. That doesn't make it a better solution to this problem. On the contrary, that makes it confusing as people will expect it to do that other stuff, and not just be there to keep the directory.

      – FrederikVds
      yesterday













    • I tried editing the answer to include NobodyNada's point, but the edit was rejected. Hopefully nobody misses the comment and loses data.

      – chicks
      17 hours ago














    5












    5








    5







    Add the following .gitignore file to the folders you wish to include in your repo.



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


    Adding .gitkeep works but isn’t an official solution. See https://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep






    share|improve this answer













    Add the following .gitignore file to the folders you wish to include in your repo.



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


    Adding .gitkeep works but isn’t an official solution. See https://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    sunknudsensunknudsen

    2093 bronze badges




    2093 bronze badges











    • 23





      This will indeed ensure Git tracks the empty directory, but it will also prevent Git from tracking any other files in that directory — so this is for if you want a directory that should always stay empty in Git.

      – NobodyNada
      2 days ago






    • 2





      I think @NobodyNada's comment should be part of this answer. Some people might want their Git project to contain a directory that is empty and never tracked (like a build directory?)

      – TomTsagk
      yesterday






    • 14





      There's no such thing as an "official" solution. You just can't have empty directories in git, period. Adding a file, any file to the directory works because then it's no longer an empty directory. This is perfectly documented, "official" behaviour. .gitignore is in the documentation, unlike other file names you might use, because it does unrelated stuff: make git ignore specific files. That doesn't make it a better solution to this problem. On the contrary, that makes it confusing as people will expect it to do that other stuff, and not just be there to keep the directory.

      – FrederikVds
      yesterday













    • I tried editing the answer to include NobodyNada's point, but the edit was rejected. Hopefully nobody misses the comment and loses data.

      – chicks
      17 hours ago














    • 23





      This will indeed ensure Git tracks the empty directory, but it will also prevent Git from tracking any other files in that directory — so this is for if you want a directory that should always stay empty in Git.

      – NobodyNada
      2 days ago






    • 2





      I think @NobodyNada's comment should be part of this answer. Some people might want their Git project to contain a directory that is empty and never tracked (like a build directory?)

      – TomTsagk
      yesterday






    • 14





      There's no such thing as an "official" solution. You just can't have empty directories in git, period. Adding a file, any file to the directory works because then it's no longer an empty directory. This is perfectly documented, "official" behaviour. .gitignore is in the documentation, unlike other file names you might use, because it does unrelated stuff: make git ignore specific files. That doesn't make it a better solution to this problem. On the contrary, that makes it confusing as people will expect it to do that other stuff, and not just be there to keep the directory.

      – FrederikVds
      yesterday













    • I tried editing the answer to include NobodyNada's point, but the edit was rejected. Hopefully nobody misses the comment and loses data.

      – chicks
      17 hours ago








    23




    23





    This will indeed ensure Git tracks the empty directory, but it will also prevent Git from tracking any other files in that directory — so this is for if you want a directory that should always stay empty in Git.

    – NobodyNada
    2 days ago





    This will indeed ensure Git tracks the empty directory, but it will also prevent Git from tracking any other files in that directory — so this is for if you want a directory that should always stay empty in Git.

    – NobodyNada
    2 days ago




    2




    2





    I think @NobodyNada's comment should be part of this answer. Some people might want their Git project to contain a directory that is empty and never tracked (like a build directory?)

    – TomTsagk
    yesterday





    I think @NobodyNada's comment should be part of this answer. Some people might want their Git project to contain a directory that is empty and never tracked (like a build directory?)

    – TomTsagk
    yesterday




    14




    14





    There's no such thing as an "official" solution. You just can't have empty directories in git, period. Adding a file, any file to the directory works because then it's no longer an empty directory. This is perfectly documented, "official" behaviour. .gitignore is in the documentation, unlike other file names you might use, because it does unrelated stuff: make git ignore specific files. That doesn't make it a better solution to this problem. On the contrary, that makes it confusing as people will expect it to do that other stuff, and not just be there to keep the directory.

    – FrederikVds
    yesterday







    There's no such thing as an "official" solution. You just can't have empty directories in git, period. Adding a file, any file to the directory works because then it's no longer an empty directory. This is perfectly documented, "official" behaviour. .gitignore is in the documentation, unlike other file names you might use, because it does unrelated stuff: make git ignore specific files. That doesn't make it a better solution to this problem. On the contrary, that makes it confusing as people will expect it to do that other stuff, and not just be there to keep the directory.

    – FrederikVds
    yesterday















    I tried editing the answer to include NobodyNada's point, but the edit was rejected. Hopefully nobody misses the comment and loses data.

    – chicks
    17 hours ago





    I tried editing the answer to include NobodyNada's point, but the edit was rejected. Hopefully nobody misses the comment and loses data.

    – chicks
    17 hours ago











    2














    Git does not track folders. In order to refrain from accumulating messes, when the index changes from (indirectly) referencing a directory to not referencing it (by doing something like git rm on the last registered file in the directory), Git tries removing the directory before it loses track of it by it no longer being in the index. If Git is successful because the directory has indeed become empty (and does not contain unregistered files), the directory will be gone, if not, it will stick around since Git no longer has notice of it.






    share|improve this answer






























      2














      Git does not track folders. In order to refrain from accumulating messes, when the index changes from (indirectly) referencing a directory to not referencing it (by doing something like git rm on the last registered file in the directory), Git tries removing the directory before it loses track of it by it no longer being in the index. If Git is successful because the directory has indeed become empty (and does not contain unregistered files), the directory will be gone, if not, it will stick around since Git no longer has notice of it.






      share|improve this answer




























        2












        2








        2







        Git does not track folders. In order to refrain from accumulating messes, when the index changes from (indirectly) referencing a directory to not referencing it (by doing something like git rm on the last registered file in the directory), Git tries removing the directory before it loses track of it by it no longer being in the index. If Git is successful because the directory has indeed become empty (and does not contain unregistered files), the directory will be gone, if not, it will stick around since Git no longer has notice of it.






        share|improve this answer













        Git does not track folders. In order to refrain from accumulating messes, when the index changes from (indirectly) referencing a directory to not referencing it (by doing something like git rm on the last registered file in the directory), Git tries removing the directory before it loses track of it by it no longer being in the index. If Git is successful because the directory has indeed become empty (and does not contain unregistered files), the directory will be gone, if not, it will stick around since Git no longer has notice of it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago







        user1077573

































            0














            Git might delete folders if you ran git clean with -dffx (possibly even with a subset of those options).



            As @TRiG mentions in the comments, if the directory is empty when you commit, it won’t show up in, e.g., GitHub, because git doesn’t track directories (it track files)—the tree where those files were is no longer relevant because there are no files, so there is no tree.



            Without more detail, it’s practically impossible for us to properly answer your underlying questions (what happened?).






            share|improve this answer










            New contributor



            D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















            • The OP "deleted all files from a directory" and committed those changes. So I believe it's quite clear what happened.

              – slhck
              yesterday











            • @slhck not so—how did they delete? rm -rf? git clean? Drag and drop to the recycle bin? What does « checked my project » mean, and which directory was gone?

              – D. Ben Knoble
              yesterday











            • To me, it's pretty reasonable to assume that it was rm foo/* (or any other method of deleting), git add foo, then commit and push. When you check your project on GitHub (as the OP indicated), the directory will be gone.

              – slhck
              yesterday











            • @slhck i see a lot of assumptions—the deletion, as mentioned. Checking on GitHub? Where is that mentioned? I prefer not to assume I know the steps to reproduce something until I know them—ive been blinded to the true cause of bugs that way before.

              – D. Ben Knoble
              yesterday






            • 2





              Deleting all files from a directly, not running git clean -d or similar, comitting, and pushing will have the observed effect (the empty directory still exists locally, but has disappeared from the remote project).

              – TRiG
              yesterday
















            0














            Git might delete folders if you ran git clean with -dffx (possibly even with a subset of those options).



            As @TRiG mentions in the comments, if the directory is empty when you commit, it won’t show up in, e.g., GitHub, because git doesn’t track directories (it track files)—the tree where those files were is no longer relevant because there are no files, so there is no tree.



            Without more detail, it’s practically impossible for us to properly answer your underlying questions (what happened?).






            share|improve this answer










            New contributor



            D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















            • The OP "deleted all files from a directory" and committed those changes. So I believe it's quite clear what happened.

              – slhck
              yesterday











            • @slhck not so—how did they delete? rm -rf? git clean? Drag and drop to the recycle bin? What does « checked my project » mean, and which directory was gone?

              – D. Ben Knoble
              yesterday











            • To me, it's pretty reasonable to assume that it was rm foo/* (or any other method of deleting), git add foo, then commit and push. When you check your project on GitHub (as the OP indicated), the directory will be gone.

              – slhck
              yesterday











            • @slhck i see a lot of assumptions—the deletion, as mentioned. Checking on GitHub? Where is that mentioned? I prefer not to assume I know the steps to reproduce something until I know them—ive been blinded to the true cause of bugs that way before.

              – D. Ben Knoble
              yesterday






            • 2





              Deleting all files from a directly, not running git clean -d or similar, comitting, and pushing will have the observed effect (the empty directory still exists locally, but has disappeared from the remote project).

              – TRiG
              yesterday














            0












            0








            0







            Git might delete folders if you ran git clean with -dffx (possibly even with a subset of those options).



            As @TRiG mentions in the comments, if the directory is empty when you commit, it won’t show up in, e.g., GitHub, because git doesn’t track directories (it track files)—the tree where those files were is no longer relevant because there are no files, so there is no tree.



            Without more detail, it’s practically impossible for us to properly answer your underlying questions (what happened?).






            share|improve this answer










            New contributor



            D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            Git might delete folders if you ran git clean with -dffx (possibly even with a subset of those options).



            As @TRiG mentions in the comments, if the directory is empty when you commit, it won’t show up in, e.g., GitHub, because git doesn’t track directories (it track files)—the tree where those files were is no longer relevant because there are no files, so there is no tree.



            Without more detail, it’s practically impossible for us to properly answer your underlying questions (what happened?).







            share|improve this answer










            New contributor



            D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.








            share|improve this answer



            share|improve this answer








            edited yesterday





















            New contributor



            D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.








            answered 2 days ago









            D. Ben KnobleD. Ben Knoble

            1015 bronze badges




            1015 bronze badges




            New contributor



            D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




            New contributor




            D. Ben Knoble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • The OP "deleted all files from a directory" and committed those changes. So I believe it's quite clear what happened.

              – slhck
              yesterday











            • @slhck not so—how did they delete? rm -rf? git clean? Drag and drop to the recycle bin? What does « checked my project » mean, and which directory was gone?

              – D. Ben Knoble
              yesterday











            • To me, it's pretty reasonable to assume that it was rm foo/* (or any other method of deleting), git add foo, then commit and push. When you check your project on GitHub (as the OP indicated), the directory will be gone.

              – slhck
              yesterday











            • @slhck i see a lot of assumptions—the deletion, as mentioned. Checking on GitHub? Where is that mentioned? I prefer not to assume I know the steps to reproduce something until I know them—ive been blinded to the true cause of bugs that way before.

              – D. Ben Knoble
              yesterday






            • 2





              Deleting all files from a directly, not running git clean -d or similar, comitting, and pushing will have the observed effect (the empty directory still exists locally, but has disappeared from the remote project).

              – TRiG
              yesterday



















            • The OP "deleted all files from a directory" and committed those changes. So I believe it's quite clear what happened.

              – slhck
              yesterday











            • @slhck not so—how did they delete? rm -rf? git clean? Drag and drop to the recycle bin? What does « checked my project » mean, and which directory was gone?

              – D. Ben Knoble
              yesterday











            • To me, it's pretty reasonable to assume that it was rm foo/* (or any other method of deleting), git add foo, then commit and push. When you check your project on GitHub (as the OP indicated), the directory will be gone.

              – slhck
              yesterday











            • @slhck i see a lot of assumptions—the deletion, as mentioned. Checking on GitHub? Where is that mentioned? I prefer not to assume I know the steps to reproduce something until I know them—ive been blinded to the true cause of bugs that way before.

              – D. Ben Knoble
              yesterday






            • 2





              Deleting all files from a directly, not running git clean -d or similar, comitting, and pushing will have the observed effect (the empty directory still exists locally, but has disappeared from the remote project).

              – TRiG
              yesterday

















            The OP "deleted all files from a directory" and committed those changes. So I believe it's quite clear what happened.

            – slhck
            yesterday





            The OP "deleted all files from a directory" and committed those changes. So I believe it's quite clear what happened.

            – slhck
            yesterday













            @slhck not so—how did they delete? rm -rf? git clean? Drag and drop to the recycle bin? What does « checked my project » mean, and which directory was gone?

            – D. Ben Knoble
            yesterday





            @slhck not so—how did they delete? rm -rf? git clean? Drag and drop to the recycle bin? What does « checked my project » mean, and which directory was gone?

            – D. Ben Knoble
            yesterday













            To me, it's pretty reasonable to assume that it was rm foo/* (or any other method of deleting), git add foo, then commit and push. When you check your project on GitHub (as the OP indicated), the directory will be gone.

            – slhck
            yesterday





            To me, it's pretty reasonable to assume that it was rm foo/* (or any other method of deleting), git add foo, then commit and push. When you check your project on GitHub (as the OP indicated), the directory will be gone.

            – slhck
            yesterday













            @slhck i see a lot of assumptions—the deletion, as mentioned. Checking on GitHub? Where is that mentioned? I prefer not to assume I know the steps to reproduce something until I know them—ive been blinded to the true cause of bugs that way before.

            – D. Ben Knoble
            yesterday





            @slhck i see a lot of assumptions—the deletion, as mentioned. Checking on GitHub? Where is that mentioned? I prefer not to assume I know the steps to reproduce something until I know them—ive been blinded to the true cause of bugs that way before.

            – D. Ben Knoble
            yesterday




            2




            2





            Deleting all files from a directly, not running git clean -d or similar, comitting, and pushing will have the observed effect (the empty directory still exists locally, but has disappeared from the remote project).

            – TRiG
            yesterday





            Deleting all files from a directly, not running git clean -d or similar, comitting, and pushing will have the observed effect (the empty directory still exists locally, but has disappeared from the remote project).

            – TRiG
            yesterday










            vitaliy4us is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            vitaliy4us is a new contributor. Be nice, and check out our Code of Conduct.













            vitaliy4us is a new contributor. Be nice, and check out our Code of Conduct.












            vitaliy4us is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1472515%2fdoes-git-delete-empty-folders%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...