关于将现有源代码添加到 GitHub
如果计算机或专用网络上已有本地存储的源代码或存储库,可以通过在终端中键入命令来将其添加到 GitHub 中。 也可以通过直接键入 Git 命令或使用 GitHub CLI 来执行此操作。
GitHub CLI 是用于从计算机的命令行使用 GitHub 的开源工具。 GitHub CLI 可以简化使用命令行将现有项目添加到 GitHub 的过程。 要详细了解 GitHub CLI,请参阅“关于 GitHub CLI”。
提示:如果你最喜欢点按式用户界面,请尝试使用 GitHub Desktop 添加项目。 有关详细信息,请参阅“GitHub Desktop 帮助”中的将存储从本地计算机添加到 GitHub 桌面。
警告:从不 git add``commit
或 push
敏感信息到远程存储库。 敏感信息包括但不限于:
- 密码
- SSH 密钥
- AWS 访问密钥
- API 密钥
- 信用卡号
- PIN 号码
有关详细信息,请参阅“从存储库中删除敏感数据”。
使用 GitHub CLI 将本地存储库添加到 GitHub
-
在命令行中,导航到项目的根目录。
-
将本地目录初始化为 Git 仓库。
git init -b main
-
暂存并提交项目中的所有文件
git add . && git commit -m "initial commit"
-
要为 GitHub 上的项目创建存储库,请使用
gh repo create
子命令。 出现提示时,选择“将现有本地存储库推送到 GitHub”,并输入存储库所需的名称。 如果希望项目属于某个组织而不是你的用户帐户,请使用organization-name/project-name
指定组织名称和项目名称。 -
按照交互式提示进行操作。 要添加远程并推送存储库,请在被要求添加远程并将提交推送到当前分支时确认“是”。
-
或者,若要跳过提示,请使用
--source
标志提供存储库的路径,并传递可见性标志(--public
、--private
或--internal
)。 例如gh repo create --source=. --public
。 使用--remote
标志指定远程。 要推送提交,请传递--push
标志。 有关可能的参数的详细信息,请参阅 GitHub CLI 手册。
将本地存储库添加到 GitHub using Git
- 在 上新建存储库。 为避免错误,请勿使用 README、许可或
gitignore
文件初始化新存储库。 您可以在项目推送到 GitHub 之后添加这些文件。1. 打开终端终端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' 并再次提交和添加文件。
- 在仓库顶部 的快速设置页面,点击 以复制远程仓库 URL。
- 在终端中,添加远程存储库的 URL(将在其中推送本地存储库)。
$ git remote add origin <REMOTE_URL> # Sets the new remote $ git remote -v # Verifies the new remote URL
- 将本地存储库中的更改推送到 。
$ git push -u origin main # Pushes the changes in your local repository up to the remote repository you specified as the origin
- 在 上新建存储库。 为避免错误,请勿使用 README、许可或
gitignore
文件初始化新存储库。 您可以在项目推送到 GitHub 之后添加这些文件。1. 打开终端终端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' 并再次提交和添加文件。
- 在仓库顶部 的快速设置页面,点击 以复制远程仓库 URL。
- 在命令提示中,添加远程存储库的 URL(将在其中推送本地存储库)。
$ git remote add origin <REMOTE_URL> # Sets the new remote $ git remote -v # Verifies the new remote URL
- 将本地存储库中的更改推送到 。
$ git push origin main # Pushes the changes in your local repository up to the remote repository you specified as the origin
- 在 上新建存储库。 为避免错误,请勿使用 README、许可或
gitignore
文件初始化新存储库。 您可以在项目推送到 GitHub 之后添加这些文件。1. 打开终端终端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' 并再次提交和添加文件。
- 在仓库顶部 的快速设置页面,点击 以复制远程仓库 URL。
- 在终端中,添加远程存储库的 URL(将在其中推送本地存储库)。
$ git remote add origin <REMOTE_URL> # Sets the new remote $ git remote -v # Verifies the new remote URL
- 将本地存储库中的更改推送到 。
$ git push origin main # Pushes the changes in your local repository up to the remote repository you specified as the origin