Skip to main content

Adding a file to a repository

You can upload and commit an existing file to a repository on GitHub or by using the command line.

Adding a file to a repository on GitHub

Files that you add to a repository via a browser are limited to 25 MB per file. You can add larger files, up to 100 MB each, via the command line. For more information, see "Adding a file to a repository using the command line." To add files larger than 100 MB, you must use Git Large File Storage. For more information, see "About large files on GitHub."

Tips:

  • You can upload multiple files to GitHub at the same time.
  • 如果仓库中有任何受保护分支,则无法使用 GitHub 编辑或上传受保护分支中的文件。 有关详细信息,请参阅“关于受保护的分支”。

您可以使用 GitHub Desktop 将您的更改转移到新的分支,然后提交它们。 有关详细信息,请参阅“提交和审查对项目的更改”。

  1. On GitHub.com, navigate to the main page of the repository.
  2. Above the list of files, using the Add file drop-down, click Upload files. "Upload files" in the "Add file" dropdown
  3. Drag and drop the file or folder you'd like to upload to your repository onto the file tree. Drag and drop area
  4. 在页面底部,输入一条简短、有意义的提交消息,描述您对文件所作的更改。 您可以在提交消息中将提交归于多个作者。 有关详细信息,请参阅“创建具有多个共同作者的提交”。 更改的提交消息
  5. 在提交消息字段下面,确定是要将提交添加到当前分支还是新分支。 如果当前分支是默认分支,则应选择为提交创建新分支,然后创建拉取请求。 有关详细信息,请参阅“创建新的拉取请求”。 提交分支选项
  6. Click Commit changes. Commit changes button

Adding a file to a repository using the command line

You can upload an existing file to a repository on GitHub.com using the command line.

此过程假设您已经:

警告:从不 git add``commitpush 敏感信息到远程存储库。 敏感信息包括但不限于:

有关详细信息,请参阅“从存储库中删除敏感数据”。

  1. On your computer, move the file you'd like to upload to GitHub into the local directory that was created when you cloned the repository.
  2. 打开终端终端Git Bash
  3. 将当前工作目录更改为您的本地仓库。
  4. 将要提交的文件暂存到本地仓库。
    $ git add .
    # Adds the file to your local repository and stages it for commit. 若要取消暂存文件,请使用“git reset HEAD YOUR-FILE”。
  5. 提交暂存在本地仓库中的文件。
    $ git commit -m "Add existing file"
    # Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。
  6. Push the changes in your local repository to GitHub.com.
    $ git push origin YOUR_BRANCH
    # Pushes the changes in your local repository up to the remote repository you specified as the origin

Further reading