Categories
git gitignore

What are the differences between .gitignore and .gitkeep?

2444

What are the differences between .gitignore and .gitkeep? Are they the same thing with a different name, or do they both serve a different function?

I don’t seem to be able to find much documentation on .gitkeep.

    4280

    .gitkeep isn’t documented, because it’s not a feature of Git.

    Git cannot add a completely empty directory. People who want to track empty directories in Git have created the convention of putting files called .gitkeep in these directories. The file could be called anything; Git assigns no special significance to this name.

    There is a competing convention of adding a .gitignore file to the empty directories to get them tracked, but some people see this as confusing since the goal is to keep the empty directories, not ignore them; .gitignore is also used to list files that should be ignored by Git when looking for untracked files.

    25

    • 518

      Wouldn’t it be a better solution to put a README file in the otherwise empty subdirectory that contains a bit of information about what that subdirectory is going to be used for? It seems confusing to have a file called .gitkeep that is not actually part of git.

      – tamouse

      Jul 14, 2012 at 21:45

    • 521

      @tamouse many times, the path of the empty directory (e.g. names of the folders) is sufficient to express it’s purpose (examples: templates/cache, upload/thumbs etc). In these cases putting a readme into each of these feels redundant.

      Jan 31, 2013 at 8:33

    • 24

      people who want to keep track of empty directories should be indicating in a README that the directory has to be created or creating the directories using their build tool or whatever tool(s) require the directory to exist ;/

      – user9903

      Jul 22, 2013 at 17:06

    • 48

      @tamouse, @omouse: A .gitignore file with two lines: * and !.gitignore is more than enough clarity to convey what is going on. If more elaboration is needed, add a comment to the top of the file using the # syntax.

      – yurisich

      Sep 2, 2013 at 16:32

    • 78

      It’s worth to notice that the popular Rails framework has slightly changed this convention using .keep files instead of .gitkeep to preserve these empty folders, since git is not the only source control system that does not track empty folders. More details here: github.com/rails/rails/issues/2800

      Nov 25, 2013 at 9:43

    423

    .gitkeep is just a placeholder. A dummy file, so Git will not forget about the directory, since Git tracks only files.


    If you want an empty directory and make sure it stays ‘clean’ for Git, create a .gitignore containing the following lines within:

    # .gitignore sample
    # Ignore all files in this dir...
    *
    
    # ... except for this one.
    !.gitignore
    

    If you desire to have only one type of files being visible to Git, here is an example how to filter everything out, except .gitignore and all .txt files:

    # .gitignore to keep just .txt files
    # Filter everything...
    *
    
    # ... except the .gitignore...
    !.gitignore
    
    # ... and all text files.
    !*.txt
    

    7

    • 3

      I like this practice myself. If there was source code in these directories there would be no need for .gitkeep and general it is temp/cache/user content which during testing would be generated anyways causing you to have to also .gitignore those files

      – chrisan

      Aug 28, 2012 at 11:37

    • 1

      Why do you need ! in front of .gitignore ? Is that in order to escape the dot ?

      – Will

      Oct 5, 2012 at 21:37

    • 9

      @Will – No, the ! negates the following part, like it usually does in programming.

      – sjas

      Oct 8, 2012 at 15:02

    • 3

      There is no need to put !.gitignore in a git ignore file, either add the file then edit it, or force add it with appropriate contents (“*” to ignore everything, or nothing to simply make sure the folder exists) further example.

      – AD7six

      May 13, 2014 at 14:26


    • 16

      From your link: Since the git ignore file is already in the repo it is not necessary to not-ignore it - it is already tracked. —— If it is not, and you do not do a forceful add, you might forget about it. In trivial cases, no problem, but if it is a bigger file you might be upset. Using !.gitignore prevents you from shooting yourself in your foot. I prefer it, having burned myself in the past.

      – sjas

      May 13, 2014 at 14:44

    144

    .gitignore
    

    is a text file comprising a list of files in your directory that git will ignore or not add/update in the repository.

    .gitkeep
    

    Since Git removes or doesn’t add empty directories to a repository, .gitkeep is sort of a hack (I don’t think it’s officially named as a part of Git) to keep empty directories in the repository.

    Just do a touch /path/to/emptydirectory/.gitkeep to add the file, and Git will now be able to maintain this directory in the repository.

    5

    • 22

      You can have as many .gitignores as you want, if you do not want to specify the full path to every folder every time.

      – sjas

      Sep 17, 2012 at 17:54

    • 1

      I try to mention empty directories list into .gitkeep file but it will not track empty directories, Only folder track where .gitkeep file exist. why so ?

      Feb 14, 2018 at 6:46

    • 3

      .gitkeep does not work like .gitignore. It’s not a list of directories to keep around. It is merely an empty file that lives in the directory you want to keep around. It can be named anything you want .keep, etc. So you can have a directory like /foo/bar and the gitkeep file will be /foo/bar/.gitkeep

      – Jim Munro

      Feb 19, 2018 at 4:48


    • 1

      @sjas how would having multiple .gitignores save you from specifying the full path to every folder every time? I think I’m missing something obvious.

      Aug 14, 2019 at 14:56


    • @Willwsharp Assuming you have /abc/def/somefile. If your git is in /abc/, you would have to ignore ./dev/somefile. If you place your gitgnore inside /abc/def, then you only need to ignore ./somefile

      – Default

      Jul 11, 2020 at 15:19