关于将现有项目添加到 GitHub
提示:如果您最喜欢点按式用户界面,请尝试使用 GitHub Desktop 添加项目。 更多信息请参阅 GitHub Desktop 帮助中的“从本地计算机添加仓库到 GitHub Desktop”。
警告:永远不要 git 添加
、提交
或推送
敏感信息到远程仓库。 敏感信息包括但不限于:
- 密码
- SSH keys
- AWS 访问密钥
- API 密钥
- 信用卡号码
- PIN 码
更多信息请参阅“从仓库中删除敏感数据”。
通过 GitHub CLI 添加项目到 GitHub
GitHub CLI 是用于从计算机的命令行使用 GitHub 的开源工具。 GitHub CLI 可以简化使用命令行将现有项目添加到 GitHub 的过程。 要了解有关 GitHub CLI 的更多信息,请参阅“关于 GitHub CLI”。
-
在命令行中,导航到项目的根目录。
-
将本地目录初始化为 Git 仓库。
git init -b main
-
要在 GitHub 上为项目创建仓库,请使用
gh repo create
子命令。 Replaceproject-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 withorganization-name/project-name
.gh repo create project-name
-
Follow the interactive prompts. Alternatively, you can specify arguments to skip these prompts. For more information about possible arguments, see the GitHub CLI manual.
-
从您创建的新仓库中拉取更改。 (如果您在前一步中创建了
.gitignore
或LICENSE
文件,这会将这些更改拉取到您的本地目录。)git pull --set-upstream origin main
-
暂存、提交和推送项目中的所有文件。
git add . && git commit -m "initial commit" && git push
不使用 GitHub CLI 而添加项目到 GitHub
- 在 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 master # 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 master # Pushes the changes in your local repository up to the remote repository you specified as the origin
延伸阅读
- "添加文件到仓库"