585

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

Luc
  • 4,264
  • 2
  • 43
  • 44
Sagar
  • 6,343
  • 6
  • 19
  • 35
  • 7
    Possible duplicate of [Creating folders inside github.com repo without using Git](https://stackoverflow.com/questions/18773598/creating-folders-inside-github-com-repo-without-using-git) – Melebius Dec 06 '17 at 06:31
  • 5
    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. https://stackoverflow.com/questions/18773598/creating-folders-on-github-com-without-using-git – Nikhilesh Mar 03 '14 at 10:54
  • @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. – M.Innat Jun 16 '18 at 15:03
  • @iPython AFAIK the duplicate target should rather be the clearer question and/or with more useful answers. The time criterion is not so important. – Melebius Jun 18 '18 at 06:37
  • https://github.com/KirstieJane/STEMMRoleModels/wiki/Creating-new-folders-in-GitHub-repository-via-the-browser – Yusril Maulidan Raji Apr 04 '19 at 11:31
  • Another Stack Overflow question, [*"Creating folders inside a GitHub repository without using Git"*, has an answer for doing it](https://stackoverflow.com/questions/18773598/creating-folders-inside-a-github-repository-without-using-git/18791455#18791455) ***only using the web interface***. – Peter Mortensen Oct 06 '19 at 13:05

14 Answers14

497

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 creation of a folder must happen together with adding 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.
Sнаđошƒаӽ
  • 15,289
  • 12
  • 72
  • 86
  • The + dropdown shows "new directory". Perhaps they recently added this?? – jim Apr 01 '21 at 14:48
  • 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. " – Denver Thomas May 07 '21 at 16:50
256

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
moopet
  • 5,804
  • 1
  • 29
  • 35
  • 1
    can you tell me the push command. I use `git push doc master`. It shows me error. – Sagar Sep 04 '12 at 08:26
  • 4
    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. – moopet Sep 04 '12 at 08:39
  • 7
    This is a better solution: https://github.com/KirstieJane/STEMMRoleModels/wiki/Creating-new-folders-in-GitHub-repository-via-the-browser – Gabriel Ferraz Jun 06 '18 at 18:04
  • 1
    @GabrielFerraz that is not a better solution, it's the same solution, just using the GitHub GUI! – moopet Jun 07 '18 at 12:06
  • 1
    @moopet Maybe not better (since the results are the same) but easier and faster. – Gabriel Ferraz Jun 07 '18 at 16:10
  • I would challenge you to go to the website and fill all that in before I could type `touch folder/foo; git commit -am "bar"; git push origin` – moopet Jun 08 '18 at 08:20
  • Committing from command prompt was good. But I like the browser approach suggested by Gabriel. The browser way of doing things did not require me to get the repository locally. I was able to add a file "README.md" inside a folder, followed by drag and drop of few image files and source codes into the newly created folder. BUT if I already have the repository locally available, then command line way of doing things is faster. The next answer from "Sнаđошƒаӽ" re-explains the browser way of creating folders. – Chinmay Apr 27 '20 at 17:28
  • @Chinmay committing from the command line is the generic approach. It'll work in five years no matter how the GUI changes. – moopet Apr 28 '20 at 19:09
  • 1
    Completely agree with you moopet with respect to generic approach. But the presently available browser approach makes my life so easy that, I dont even need to do any download to or upload from any file, any folder onto my local computer, I can do everything in place at GitHub website directly. Saying so, I must acknowledge again that if I already have a local git repo setup, then command line approach is preferred. – Chinmay Apr 28 '20 at 21:59
52

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 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.
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
nevosial
  • 814
  • 10
  • 15
42

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Kien Truong
  • 10,866
  • 2
  • 28
  • 34
  • 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. – Carlo Carandang Apr 19 '21 at 02:31
29

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

aheze
  • 16,189
  • 4
  • 33
  • 81
Shivanandam
  • 1,714
  • 1
  • 21
  • 25
8

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

Java/Helloworld.txt

J.Doe
  • 111
  • 2
  • 6
3

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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Mohanraj
  • 3,834
  • 1
  • 20
  • 24
  • 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 ` then `git push foo master`. Last command show me a error. Where am I wrong? – Sagar Sep 04 '12 at 08:10
  • 1
    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" – Mohanraj Sep 04 '12 at 09:19
3

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
saurav singh
  • 363
  • 5
  • 14
3

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

Krishnadas PC
  • 4,900
  • 1
  • 45
  • 40
2

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

Ramesh Boosa
  • 148
  • 8
1

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.

1

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.
Nitish770
  • 371
  • 2
  • 8
0

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Zuha Karim
  • 249
  • 4
  • 11
0

Just drag the folder from the Windows Explorer into your Browser where the github Upload Files is shown - now the folder will be add automatically ...

Ensai Tankado
  • 89
  • 1
  • 4