如何通过GitHub网页GUI向仓库添加文件夹?无文件创建方法咨询
Hey there, I totally get where you're coming from—this is a common gotcha with Git and GitHub! Let's break this down simply:
Key Background First
Important: Git (and by extension GitHub) doesn't track empty folders. A folder will only appear in your repository if it contains at least one file. That's why you can't create a completely empty folder directly via the web GUI—there's nothing for Git to "hold onto" and track.
The Workaround: Use a Placeholder File
If you need a folder that appears empty (but technically has a tiny placeholder), here's how to do it via the web GUI:
- Navigate to your repository's main page on GitHub.
- Click the Add file dropdown menu and select Create new file.
- In the file name input field, type the name of your desired folder followed by a slash (e.g.,
my-new-folder/). You'll see the folder name pop up as a nested path immediately. - Next, type a placeholder file name inside that folder. The standard convention is
.gitkeep—this isn't a special Git file, it's just a common name to signal "keep this folder even if it's otherwise empty." - Add a short commit message (like "Add empty folder with .gitkeep") and click Commit new file.
- Your folder will now show up in the repository, with the
.gitkeepfile sitting inside it.
The Method You Already Know (For Folders with Files)
Just to recap the approach you mentioned: if you're planning to add a file to the folder anyway, you can skip the placeholder. When creating a new file, just type the folder path plus your file name (e.g., my-new-folder/document.txt)—GitHub will automatically create the folder along with the file.
Quick Note for Later
If you ever want to remove the .gitkeep file, make sure you add another file to the folder first. Otherwise, once the placeholder is gone, Git will stop tracking the folder entirely, and it will disappear from your repository.
内容的提问来源于stack exchange,提问作者Michael Angel

