How do I create a folder in a GitHub repository?

Learn how do i create a folder in a github repository? with practical examples, diagrams, and best practices. Covers github, github-services development techniques with visual explanations.

How to Create a Folder in a GitHub Repository

Hero image for How do I create a folder in a GitHub repository?

Learn various methods to create new folders (directories) within your GitHub repositories, both directly through the web interface and using Git commands locally.

Organizing your GitHub repository with a clear folder structure is crucial for project maintainability and collaboration. Whether you're adding new features, separating documentation, or structuring your code, knowing how to create folders efficiently is a fundamental skill. This article will guide you through the different ways to achieve this, catering to both direct web-based creation and local Git workflows.

Understanding GitHub's Folder Creation Logic

GitHub's web interface doesn't have a direct 'Create Folder' button. Instead, it infers folder creation when you create a new file and specify a path that includes a directory name. For example, if you create a file named docs/README.md, GitHub automatically creates the docs folder if it doesn't already exist, and places README.md inside it. This behavior is consistent across both the web interface and local Git operations.

flowchart TD
    A[Start: User wants to create a folder] --> B{Choose Method}
    B --> C[Method 1: GitHub Web Interface]
    B --> D[Method 2: Local Git Commands]

    C --> C1["Navigate to Repository"]
    C1 --> C2["Click 'Add file' -> 'Create new file'"]
    C2 --> C3["Type 'folder_name/file_name.ext'"]
    C3 --> C4["Add content & Commit new file"]
    C4 --> C_End[End: Folder created with file]

    D --> D1["Clone/Navigate to Local Repo"]
    D1 --> D2["Create folder locally: `mkdir folder_name`"]
    D2 --> D3["Create file inside: `touch folder_name/file_name.ext`"]
    D3 --> D4["Add, Commit, Push: `git add .`, `git commit -m 'Add new folder'`, `git push`"]
    D4 --> D_End[End: Folder created and pushed]

Workflow for creating folders in GitHub

Method 1: Creating a Folder via the GitHub Web Interface

This is the simplest method for quick additions, especially for small files or when you don't have your local development environment set up. Remember, you cannot create an empty folder directly; it must contain at least one file.

1. Navigate to Your Repository

Open your web browser and go to the GitHub repository where you want to create the new folder.

2. Initiate New File Creation

On the repository's main page, click the 'Add file' dropdown button, then select 'Create new file'.

3. Specify Folder and File Name

In the file name field, type the desired folder name followed by a forward slash /, and then the name of a file to place inside it. For example, to create a folder named my-new-folder with a file placeholder.txt inside, you would type my-new-folder/placeholder.txt. GitHub will automatically recognize my-new-folder as a new directory.

4. Add Content and Commit

Add some content to the file (even a single character is enough). Then, scroll down, provide a commit message (e.g., 'Create my-new-folder and placeholder file'), and click 'Commit new file'.

Method 2: Creating a Folder Using Local Git Commands

For more complex projects or when you're already working locally, using Git commands is the standard and most flexible approach. This method allows you to create multiple folders and files before pushing your changes to GitHub.

1. Clone or Navigate to Your Local Repository

If you haven't already, clone the repository to your local machine using git clone <repository_url>. If it's already cloned, navigate to the repository's root directory in your terminal or command prompt.

2. Create the Folder Locally

Use the mkdir command to create your new directory. For example: mkdir my-new-folder.

Git does not track empty directories. To ensure your new folder is tracked, create at least one file inside it. A common practice is to create a .gitkeep file: touch my-new-folder/.gitkeep.

4. Stage, Commit, and Push Your Changes

Add the new folder and its contents to your staging area: git add my-new-folder. Then, commit your changes: git commit -m "Add new folder: my-new-folder". Finally, push your local changes to the remote GitHub repository: git push origin main (or master, depending on your branch name).

git clone https://github.com/your-username/your-repo.git
cd your-repo
mkdir new-feature-folder
touch new-feature-folder/.gitkeep
git add new-feature-folder
git commit -m "Add new-feature-folder with .gitkeep"
git push origin main

Example Git commands to create and push a new folder