About adding existing source code to GitHub
If you have existing source code or repositories stored locally on your computer or private network you can add them to GitHub by typing commands in a terminal. You can do this by typing Git commands directly, or by using GitHub CLI.
GitHub CLI 是用于从计算机的命令行使用 GitHub 的开源工具。 GitHub CLI 可以简化使用命令行将现有项目添加到 GitHub 的过程。 要了解有关 GitHub CLI 的更多信息,请参阅“关于 GitHub CLI”。
提示:如果您最喜欢点按式用户界面,请尝试使用 GitHub Desktop 添加项目。 更多信息请参阅 GitHub Desktop 帮助中的“从本地计算机添加仓库到 GitHub Desktop”。
警告:永远不要 git 添加
、提交
或推送
敏感信息到远程仓库。 敏感信息包括但不限于:
- 密码
- SSH keys
- AWS 访问密钥
- API 密钥
- 信用卡号码
- PIN 码
更多信息请参阅“从仓库中删除敏感数据”。
Adding a local repository to GitHub with GitHub CLI
-
在命令行中,导航到项目的根目录。
-
将本地目录初始化为 Git 仓库。
git init -b main
-
Stage and commit all the files in your project
git add . && git commit -m "initial commit"
-
To create a repository for your project on GitHub, use the
gh repo create
subcommand. When prompted, select Push an existing local repository to GitHub and enter the desired name for your repository. If you want your project to belong to an organization instead of your user account, specify the organization name and project name withorganization-name/project-name
. -
按照交互式提示进行操作。 To add the remote and push the repository, confirm yes when asked to add the remote and push the commits to the current branch.
-
Alternatively, to skip all the prompts, supply the path to the repository with the
--source
flag and pass a visibility flag (--public
,--private
, or--internal
). For example,gh repo create --source=. --public
. Specify a remote with the--remote
flag. To push your commits, pass the--push
flag. 有关可能的参数的详细信息,请参阅 GitHub CLI 手册。
Adding a local repository to GitHub using Git
- 在 GitHub.com 上创建新仓库 为避免错误,请勿使用自述文件、许可或
gitignore
文件初始化新仓库。 您可以在项目推送到 GitHub 之后添加这些文件。 - 打开 Terminal(终端)Terminal(终端)Git Bash。
- 将当前工作目录更改为您的本地仓库。
- 将本地目录初始化为 Git 仓库。
$ git init -b main
- 在新的本地仓库中添加文件。 这会暂存它们用于第一次提交。
$ git add . # Adds the files in the local repository and stages them for commit. 要取消暂存文件,请使用 'git reset HEAD YOUR-FILE'。
- 提交暂存在本地仓库中的文件。
$ git commit -m "First commit" # Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。
- 在仓库顶部 GitHub.com 的快速设置页面,点击 以复制远程仓库 URL。
- 在终端上,添加远程仓库的 URL(将在该 URL 推送本地仓库)。
$ git remote add origin <REMOTE_URL> # Sets the new remote $ git remote -v # Verifies the new remote URL
- 推送更改(本地仓库中)到 GitHub.com。
$ git push -u origin main # Pushes the changes in your local repository up to the remote repository you specified as the origin
- 在 GitHub.com 上创建新仓库 为避免错误,请勿使用自述文件、许可或
gitignore
文件初始化新仓库。 您可以在项目推送到 GitHub 之后添加这些文件。 - 打开 Terminal(终端)Terminal(终端)Git Bash。
- 将当前工作目录更改为您的本地仓库。
- 将本地目录初始化为 Git 仓库。
$ git init -b main
- 在新的本地仓库中添加文件。 这会暂存它们用于第一次提交。
$ git add . # Adds the files in the local repository and stages them for commit. 要取消暂存文件,请使用 'git reset HEAD YOUR-FILE'。
- 提交暂存在本地仓库中的文件。
$ git commit -m "First commit" # Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。
- 在仓库顶部 GitHub.com 的快速设置页面,点击 以复制远程仓库 URL。
- 在命令提示中,添加远程仓库的 URL(将在该 URL 推送本地仓库)。
$ git remote add origin <REMOTE_URL> # Sets the new remote $ git remote -v # Verifies the new remote URL
- 推送更改(本地仓库中)到 GitHub.com。
$ git push origin main # Pushes the changes in your local repository up to the remote repository you specified as the origin
- 在 GitHub.com 上创建新仓库 为避免错误,请勿使用自述文件、许可或
gitignore
文件初始化新仓库。 您可以在项目推送到 GitHub 之后添加这些文件。 - 打开 Terminal(终端)Terminal(终端)Git Bash。
- 将当前工作目录更改为您的本地仓库。
- 将本地目录初始化为 Git 仓库。
$ git init -b main
- 在新的本地仓库中添加文件。 这会暂存它们用于第一次提交。
$ git add . # Adds the files in the local repository and stages them for commit. 要取消暂存文件,请使用 'git reset HEAD YOUR-FILE'。
- 提交暂存在本地仓库中的文件。
$ git commit -m "First commit" # Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。
- 在仓库顶部 GitHub.com 的快速设置页面,点击 以复制远程仓库 URL。
- 在终端上,添加远程仓库的 URL(将在该 URL 推送本地仓库)。
$ git remote add origin <REMOTE_URL> # Sets the new remote $ git remote -v # Verifies the new remote URL
- 推送更改(本地仓库中)到 GitHub.com。
$ git push origin main # Pushes the changes in your local repository up to the remote repository you specified as the origin
延伸阅读
- "添加文件到仓库"