ChatGPT解决这个技术问题 Extra ChatGPT

What are the differences between .gitignore and .gitkeep?

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.


K
Konrad Rudolph

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


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 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.
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 ;/
@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.
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
M
Michael

.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

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
Why do you need ! in front of .gitignore ? Is that in order to escape the dot ?
@Will - No, the ! negates the following part, like it usually does in programming.
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.
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.
P
Peter Mortensen
.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.


You can have as many .gitignores as you want, if you do not want to specify the full path to every folder every time.
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 ?
.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
@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.
@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
r
rupakg

Many people prefer to use just .keep since the convention has nothing to do with git.


arguably, the reason for the convention is almost explicitly because of git
@JDPeckham Perforce also needs files present to track a folder.
S
Sohel Ahmed Mesaniya

This is not an answer to the original question "What are the differences between .gitignore and .gitkeep?" but posting here to help people to keep track of empty dir in a simple fashion. To track empty directory and knowling that .gitkeep is not official part of git,

https://i.stack.imgur.com/Smlxv.png

just add a empty (with no content) .gitignore file in it.

So for e.g. if you have /project/content/posts and sometimes posts directory might be empty then create empty file /project/content/posts/.gitignore with no content to track that directory and its future files in git.


I disagree. The fact that .gitkeep is not an official part of git is absolutely not a reason to misuse .gitignore. An empty .gitignore is confusing and meaningless. Where as an empty .gitkeep is clearly a placeholder to "keep" the directory in git.
I believe that is your point of view not of all
To be clear, your point of view is that it's OK (or a good idea) to extend the official .gitignore file purpose so that it doubles up as a placeholder when empty? Like I said, I cannot disagree more. Perhaps you could edit your answer to explain your reasoning or back up your point of view? Right now it is a baseless answer and fully deserves my downvote.
Ah, I realize I have stumbled in to a conventions war. I just noticed the comments on the accepted answer. Maybe there are two conventions here, and maybe each has their own number of followers so that popularity differs. I would still like to hear the arguments FOR this convention because maybe I will change my mind.
I see that. But can you show in the official Git docs where it directs people to add empty .gitignore files?