The Wayback Machine - https://web.archive.org/web/20211108181939/https://docs.github.com/cn/github/importing-your-projects-to-github/importing-source-code-to-github/adding-an-existing-project-to-github-using-the-command-line

使用命令行添加现有项目到 GitHub

将您的现有工作放到 GitHub 上可通过许多很好的方式共享和协作。

关于将现有项目添加到 GitHub

提示:如果您最喜欢点按式用户界面,请尝试使用 GitHub Desktop 添加项目。 更多信息请参阅 GitHub Desktop 帮助中的“从本地计算机添加仓库到 GitHub Desktop”。

警告:永远不要 git 添加提交推送敏感信息到远程仓库。 敏感信息包括但不限于:

更多信息请参阅“从仓库中删除敏感数据”。

通过 GitHub CLI 添加项目到 GitHub

GitHub CLI 是用于从计算机的命令行使用 GitHub 的开源工具。 GitHub CLI 可以简化使用命令行将现有项目添加到 GitHub 的过程。 要了解有关 GitHub CLI 的更多信息,请参阅“关于 GitHub CLI”。

  1. 在命令行中,导航到项目的根目录。

  2. 将本地目录初始化为 Git 仓库。

    git init -b main
  3. 要在 GitHub 上为项目创建仓库,请使用 gh repo create 子命令。 Replace project-name with the desired name for your repository. If you want your project to belong to an organization instead of to your user account, specify the organization name and project name with organization-name/project-name.

    gh repo create project-name
  4. Follow the interactive prompts. Alternatively, you can specify arguments to skip these prompts. For more information about possible arguments, see the GitHub CLI manual.

  5. 从您创建的新仓库中拉取更改。 (如果您在前一步中创建了 .gitignoreLICENSE 文件,这会将这些更改拉取到您的本地目录。)

    git pull --set-upstream origin main
  6. 暂存、提交和推送项目中的所有文件。

    git add . && git commit -m "initial commit" && git push

不使用 GitHub CLI 而添加项目到 GitHub

  1. 在 GitHub.com 上创建新仓库 为避免错误,请勿使用自述文件、许可或 gitignore 文件初始化新仓库。 您可以在项目推送到 GitHub 之后添加这些文件。 创建新仓库下拉列表
  2. 打开 Terminal(终端)Terminal(终端)Git Bash
  3. 将当前工作目录更改为您的本地仓库。
  4. 将本地目录初始化为 Git 仓库。
    $ git init -b main
  5. 在新的本地仓库中添加文件。 这会暂存它们用于第一次提交。
    $ git add .
    # Adds the files in the local repository and stages them for commit. 要取消暂存文件,请使用 'git reset HEAD YOUR-FILE'。
  6. 提交暂存在本地仓库中的文件。
    $ git commit -m "First commit"
    # Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。
  7. 在仓库顶部 GitHub.com 的快速设置页面,点击 以复制远程仓库 URL。 创建远程仓库 URL 字段
  8. 在终端上,添加远程仓库的 URL(将在该 URL 推送本地仓库)。
    $ git remote add origin  <REMOTE_URL> 
    # Sets the new remote
    $ git remote -v
    # Verifies the new remote URL
  9. 推送更改(本地仓库中)到 GitHub.com。
    $ git push -u origin main
    # Pushes the changes in your local repository up to the remote repository you specified as the origin
  1. 在 GitHub.com 上创建新仓库 为避免错误,请勿使用自述文件、许可或 gitignore 文件初始化新仓库。 您可以在项目推送到 GitHub 之后添加这些文件。 创建新仓库下拉列表
  2. 打开 Terminal(终端)Terminal(终端)Git Bash
  3. 将当前工作目录更改为您的本地仓库。
  4. 将本地目录初始化为 Git 仓库。
    $ git init -b main
  5. 在新的本地仓库中添加文件。 这会暂存它们用于第一次提交。
    $ git add .
    # Adds the files in the local repository and stages them for commit. 要取消暂存文件,请使用 'git reset HEAD YOUR-FILE'。
  6. 提交暂存在本地仓库中的文件。
    $ git commit -m "First commit"
    # Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。
  7. 在仓库顶部 GitHub.com 的快速设置页面,点击 以复制远程仓库 URL。 创建远程仓库 URL 字段
  8. 在命令提示中,添加远程仓库的 URL(将在该 URL 推送本地仓库)。
    $ git remote add origin  <REMOTE_URL> 
    # Sets the new remote
    $ git remote -v
    # Verifies the new remote URL
  9. 推送更改(本地仓库中)到 GitHub.com。
    $ git push origin master
    # Pushes the changes in your local repository up to the remote repository you specified as the origin
  1. 在 GitHub.com 上创建新仓库 为避免错误,请勿使用自述文件、许可或 gitignore 文件初始化新仓库。 您可以在项目推送到 GitHub 之后添加这些文件。 创建新仓库下拉列表
  2. 打开 Terminal(终端)Terminal(终端)Git Bash
  3. 将当前工作目录更改为您的本地仓库。
  4. 将本地目录初始化为 Git 仓库。
    $ git init -b main
  5. 在新的本地仓库中添加文件。 这会暂存它们用于第一次提交。
    $ git add .
    # Adds the files in the local repository and stages them for commit. 要取消暂存文件,请使用 'git reset HEAD YOUR-FILE'。
  6. 提交暂存在本地仓库中的文件。
    $ git commit -m "First commit"
    # Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。
  7. 在仓库顶部 GitHub.com 的快速设置页面,点击 以复制远程仓库 URL。 创建远程仓库 URL 字段
  8. 在终端上,添加远程仓库的 URL(将在该 URL 推送本地仓库)。
    $ git remote add origin  <REMOTE_URL> 
    # Sets the new remote
    $ git remote -v
    # Verifies the new remote URL
  9. 推送更改(本地仓库中)到 GitHub.com。
    $ git push origin master
    # Pushes the changes in your local repository up to the remote repository you specified as the origin

延伸阅读

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或, 了解如何参与。