本文翻译自:How do I create a folder in a GitHub repository?
I want to create a folder in a GitHub repository and want to add files in that folder. 我想在GitHub存储库中创建一个文件夹,并想要在该文件夹中添加文件。 How do I achieve this? 我该如何实现?
#1楼
参考:https://stackoom.com/question/PqY7/如何在GitHub存储库中创建文件夹
#2楼
Git doesn't store empty folders . Git不存储空文件夹 。 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). 只需确保文件夹中有一个文件(如doc / foo.txt),然后运行git add doc
或git add doc/foo.txt
,提交后该文件夹将被添加到您的本地存储库中(并在GitHub上出现一次您已按下它)。
#3楼
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: 然后,您可以在local_directory
内创建本地文件夹和文件,并使用以下命令将它们添加到存储库中:
git add file_path
You can also add everything using: 您还可以使用以下方法添加所有内容:
git add .
Note that Git does not track empty folders. 请注意,Git不会跟踪空文件夹。 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. 我通常将该文件命名为empty
,但是可以选择任何名称。
Finally, you commit and push back to GitHub: 最后,提交并推回GitHub:
git commit
git push
For more information on Git, check out the Pro Git book. 有关Git的更多信息,请查阅Pro Git书。
#4楼
You just create the required folders in your local repository. 您只需在本地存储库中创建所需的文件夹。 For example, you created the app
and config
directories. 例如,您创建了app
和config
目录。
You may create new files under these folders. 您可以在这些文件夹下创建新文件。
For Git rules: 对于Git规则:
- First we need to add files to the directory. 首先,我们需要将文件添加到目录中。
- Then commit those added files. 然后提交那些添加的文件。
Git command to do commit: 用Git命令来提交:
-
git add app/ config/
-
git commit
Then give the commit message and save the commit. 然后提供提交消息并保存提交。
Then push to your remote repository, 然后推送到您的远程存储库,
git push origin remote
#5楼
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. 您不能创建一个空文件夹,然后将文件添加到该文件夹,但是创建文件夹必须与添加至少一个文件一起进行。 On GitHub you can do it this way: 在GitHub上,您可以这样操作:
- 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) 最后,给新文件起一个名字(例如,.gitkeep
, 通常用于使Git跟踪否则为空文件夹;不过它不是Git功能) - Finally, click Commit new file . 最后,点击提交新文件 。
#6楼
Create a new file, and then on the filename use slash. 创建一个新文件,然后在文件名上使用斜杠。 For example 例如
Java/Helloworld.txt Java / Helloworld.txt