How to track directories but not their files with Git?

前端 未结 5 2078
礼貌的吻别
礼貌的吻别 2020-12-02 10:22

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 wo

5条回答
  •  时光说笑
    2020-12-02 10:47

    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
    

提交回复
热议问题