722

I want to create a folder in a GitHub repository and then add files to that folder.
How do I achieve this?

5
  • 8
    I know this is very old question but still might save time for someone The below link is to an answer mentioning how to create folder on Github website itself. stackoverflow.com/questions/18773598/… Commented Mar 3, 2014 at 10:54
  • 8
    Possible duplicate of Creating folders inside github.com repo without using Git Commented Dec 6, 2017 at 6:31
  • 2
    @Melebius , I think duplicate flag should be set to others question you just mention because This question is asked first, a years ago than your mentioned. Commented Jun 16, 2018 at 15:03
  • 1
    @iPython AFAIK the duplicate target should rather be the clearer question and/or with more useful answers. The time criterion is not so important. Commented Jun 18, 2018 at 6:37
  • Got it done really fast with a /. Video walkthrough: youtu.be/IeGElVLEbao Commented Jul 11 at 1:45

18 Answers 18

634

TL;DR Use / in the file name field to create folder(s), e.g. typing folder1/file1 in the file name field will create a folder folder1 and a file file1.

Original answer
You cannot create an empty folder and then add files to that folder, but rather the creation of a folder must happen together with the addition of at least a single file. This is because git doesn't track empty folders.

On GitHub, you can do it this way:

  • Go to the folder inside which you want to create another folder
  • Click on New file
  • On the text field for the file name, first write the folder name you want to create
  • Then type /. This creates a folder
  • You can add more folders similarly
  • Finally, give the new file a name (for example, .gitkeep which is conventionally used to make Git track otherwise empty folders; it is not a Git feature though)
  • Finally, click Commit new file.
Sign up to request clarification or add additional context in comments.

5 Comments

The + dropdown shows "new directory". Perhaps they recently added this??
Whenever I try this in any combination of new folder name, new file name/extension/contents, I get the error "Sorry, a file exists where you’re trying to create a subdirectory. Choose a new path and try again. "
is there a way to do this on a command line of a terminal? thanks
@jim It doesn't show that on mine. Maybe it was an A/B test or something. Creating a new directory doesn't even really mean anything for git since git doesn't track empty directories, so IDK why that would even exist.
@Abdul Are you looking for git add? Otherwise, I'm not sure what you mean by "this" -- maybe you mean create a file and its containing directories in one step? I don't think there's a way to do that on Unix, no.
265

Git doesn't store empty folders. Just make sure there's a file in the folder like doc/foo.txt and run git add doc or git add doc/foo.txt, and the folder will be added to your local repository once you've committed (and appear on GitHub once you've pushed it).

2 Comments

can you tell me the push command. I use git push doc master. It shows me error.
If you set up your repository on github the way the site suggests, it'd be "git push origin master" - origin being the default name for the remote repository and master being the default name of your branch.
62

For the ones using the web browser, you can do the following:

  • Once in the master repository, click on Create new file.
  • In the name of the file box at the top, enter the name of your folder
  • Use the / key after the name of the folder. Using this forward slash creates the folder
  • You can see a new box appear next to the folder name wherein you can type the name of your file.
  • In the Commit new file box at the bottom of the page, you can type the description for your file.
  • Select the radio button Commit directly to the master branch.
  • Click on the Commit new file button
  • You will see the new directory will be created.

Comments

54

Simple Steps:

Step 1: Click on Create new File

Click "Create new file" under the "Add file" dropdown


Step 2: Enter the folder name that you want, then press /

Enter folder name inside text field


Step 3: Enter a sample file name. You must enter some text.

Enter sample file name


Step 4: Click Commit new file to create the folder

Click "Commit new file"


Step 5: Your folder is created!

Folder appears inside repository home page

1 Comment

Crop the screenshots please! We don't need to see all your taskbar icons and everything else. As it stands, most of the text is not even legible with the images inline.
44

First you have to clone the repository to you local machine

git clone github_url local_directory

Then you can create local folders and files inside your local_directory, and add them to the repository using:

git add file_path

You can also add everything using:

git add .

Note that Git does not track empty folders. A workaround is to create a file inside the empty folder you want to track. I usually name that file empty, but it can be whatever name you choose.

Finally, you commit and push back to GitHub:

git commit
git push

For more information on Git, check out the Pro Git book.

1 Comment

This is the "bottom-up" approach to creating a folder in a Git repo, albeit this one is creating a local folder/file and then commit and push up to remote, whereas the accepted answer is a "top-down" approach, where a folder/file are created in GitHub on the remote side.
10

Create a new file, and then on the filename use slash. For example:

Java/Helloworld.txt

3 Comments

I don't think that answers the question very clearly, or adds anything to the existing answers.
No, It was useful.
What exactly do you mean by "then on the filename use slash"? Can you elaborate? A screenshot might be useful.
5

Just drag the folder from Windows Explorer into your browser where the GitHub "Upload Files" is shown. The folder will now be added automatically.

Comments

4

Actually, GitHub does not create an empty folder.

For example, to create a folder in C:\Users\Username\Documents\GitHub\Repository:

  • Create a folder named docs

  • Create a file name index.html under docs

  • Open the GitHub for desktop application

    It will automatically sync, and it will be there.

Comments

4

You just create the required folders in your local repository. For example, you created the app and config directories.

You may create new files under these folders.

For Git rules:

  1. First we need to add files to the directory.
  2. Then commit those added files.

Git command to do commit:

  1. git add app/ config/
  2. git commit

Then give the commit message and save the commit.

Then push to your remote repository,

git push origin remote

2 Comments

i add a folder "foo" in my local repo and add a file in it. git add foo and commit it. Then git remote add foo <URL> then git push foo master. Last command show me a error. Where am I wrong?
No need to do "git remote add". After you added "git add foo/", then give "git commit", it will ask commit message give commit message. Then push using "git push origin master"
4

Click on new file in GitHub repo online. Then write the file name as myfolder/myfilename then give the file contents and commit. Then file will be created within that new folder.

Comments

3

Please follow the below steps to create Folders under the repository:

  1. Login into Github.
  2. Select your repository.
  3. Tap on "Add file" to the "Create New File" Option.
  4. Enter your Folder Name(Ex: RepositoryName/FolderName) and enter "/".
  5. Enter a file name to commit. I have created README.md for each folder so that it will be easy for me to maintain the details of every folder.
  6. Scroll down to the Commit new file section.
  7. Choose an option to merge directly to the "master" branch or "Create a new branch".
  8. Finally, You need to tap on "Commit new file".

Now, as soon as you tap on Commit new file it will create and take you back to the Repository.

Comments

2

Here is an easy and quick, presently available browser approach to creating folders inside a repository

1)Click the repository / create a new repository.

2)Click create Add file and then create a new file.

3)Give the folder name you want to create with a ' / ' mark and then add a file in it

4)Commit the changes

Click here for the visual representation of the above steps in order.

Comments

2

I don't know whenever I use "/" in repository name it is replaced by "-" maybe github changed method of creating folders.

So I'm going to tell you what I did to create a empty folder and to add files.

  1. Click on New
  2. enter your folder name and nothing else
  3. Click on "Add a README file"
  4. Click "Create Repository"
  5. Now clone the folder you created.
  6. Add files or folders in the local repo
  7. Commit changes.
  8. And there you go.

Comments

2

On GitHub you can do it this way:

Go to the folder inside which you want to create another folder Click on New file On the text field for the file name, first write the folder name you want to create Then type /. This creates a folder You can add more folders similarly enter image description here

Comments

2

To create a folder and upload a file, first create an empty file, then upload the actual file.

To create an empty file that you later upload/update – in a web browser :

  1. sign in to your GitHub account,
  2. in the top-left corner, click the desired repository,
  3. click Add file ▾ (it's to the left of the green <> Code ▾ button),
  4. click Create new file,
  5. at <RespositoryName>/, enter path/to/new-folder/the-file-name,
  6. on the right side, click the green Commit changes... button,
  7. once again, click Commit changes.

Now upload the file :

  1. at the upper right, click Add file ▾,
  2. click Upload files,
  3. click choose your files,
  4. browse to find your file(s) and choose them,
  5. scroll down and click Commit changes.

Comments

1

To add a new directory all you have to do is create a new folder in your local repository. Create a new folder, and add a file in it.

Now go to your terminal and add it like you add the normal files in Git. Push them into the repository, and check the status to make sure you have created a directory.

Comments

1

Here is my recommended simple solution:

1. Navigate to the repository where you want to create the folder.

2. Click on Add file in the top-right corner and choose Create new file.

3. Enter the folder name in the Name field, followed by a / (slash), and then type "temp.txt" as an example. Next, click on Commit changes and confirm.

After following these steps, the folder will be created. Once you have uploaded the files into the folder, you can proceed to delete the "temp.txt" file.

Comments

0

If you're using Git command line or any command line to update your repo on mac or linux machine, you can run the following commands to create the directory:

  1. Navigate to the git repo location you want to add a new folder to in command line.
  2. Run mkdir folder_name
  3. Run ls to list folder changes which should show the new folder.
  4. Add a new file to the directory. (You must add a file to the directory else git won't show anything to commit)
  5. Next run git add folder_name that contains the new file(s).
  6. git commit -m "With you message"
  7. git push
  8. Visit your repository on GitHub and notice that your changes are reflected there as well online.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.