13 KiB
title, intro, redirect_from, versions, shortTitle
| title | intro | redirect_from | versions | shortTitle | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Adding locally hosted code to GitHub | Learn how to add existing source code or repositories to {% data variables.product.product_name %} from the command line using {% data variables.product.prodname_cli %} or Git Commands. Then, share your code and invite others to work with you. |
|
|
Add locally hosted code |
About adding existing source code to {% data variables.product.product_name %}
If you have existing source code or repositories stored locally on your computer or private network you can add them to {% data variables.product.product_name %} by typing commands in a terminal. You can do this by typing Git commands directly, or by using {% data variables.product.prodname_cli %}.
{% data variables.product.prodname_cli %} は、コンピューターのコマンドラインから {% data variables.product.prodname_dotcom %} を使用するためのオープンソースツールです。 {% data variables.product.prodname_cli %} can simplify the process of adding an existing project to {% data variables.product.product_name %} using the command line. {% data variables.product.prodname_cli %}についてさらに学ぶには、「{% data variables.product.prodname_cli %}について」を参照してください。
{% tip %}
ヒント: ポイントアンドクリック型のユーザインターフェースに慣れている場合は、プロジェクトを {% data variables.product.prodname_desktop %}で追加してみてください。 詳しい情報については {% data variables.product.prodname_desktop %}ヘルプ 中のローカルコンピュータから GitHub Desktop へのリポジトリの追加を参照してください。
{% endtip %}
{% data reusables.repositories.sensitive-info-warning %}
Adding a local repository to {% data variables.product.product_name %} with {% data variables.product.prodname_cli %}
-
In the command line, navigate to the root directory of your project.
-
ローカルディレクトリを 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 createsubcommand. 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. -
Follow the interactive prompts. 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
--sourceflag and pass a visibility flag (--public,--private, or--internal). For example,gh repo create --source=. --public. Specify a remote with the--remoteflag. To push your commits, pass the--pushflag. For more information about possible arguments, see the GitHub CLI manual.
Adding a local repository to {% data variables.product.product_name %} using Git
{% mac %}
-
{% data variables.product.product_location %}上で新しいリポジトリを作成します。 エラーを避けるため、新しいリポジトリはREADME、ライセンス、あるいは
gitignoreで初期化しないでください。 これらのファイルは、プロジェクトを {% data variables.product.product_name %}にプッシュした後で追加できます。
{% data reusables.command_line.open_the_multi_os_terminal %} -
ワーキングディレクトリをローカルプロジェクトに変更します。
-
Use the
initcommand to initialize the local directory as a Git repository. By default, the initial branch is calledmaster.If you’re using Git 2.28.0 or a later version, you can set the name of the default branch using
-b.$ git init -b mainIf you’re using Git 2.27.1 or an earlier version, you can set the name of the default branch using
&& git branch -m.$ git init && git branch -m main -
ファイルを新しいローカルリポジトリに追加します。 これで、それらのファイルが最初のコミットに備えてステージングされます。
$ git add .
# ローカルリポジトリにファイルを追加し、コミットに備えてステージします。 {% data reusables.git.unstage-codeblock %}
- ローカルリポジトリでステージングしたファイルをコミットします。
$ git commit -m "First commit"
# 追跡された変更をコミットし、リモートリポジトリへのプッシュに備えます。 {% data reusables.git.reset-head-to-previous-commit-codeblock %}
- At the top of your repository on {% ifversion ghae %}{% data variables.product.product_name %}{% else %}{% data variables.product.product_location %}{% endif %}'s Quick Setup page, click {% octicon "clippy" aria-label="The copy to clipboard icon" %} to copy the remote repository URL.

- ターミナルで、ローカルリポジトリがプッシュされるリモートリポジトリの URL を追加してください。
$ git remote add origin <em> <REMOTE_URL> </em>
# 新しいリモートを設定する
$ git remote -v
# 新しいリモート URL を検証する
- {% data variables.product.product_location %} へ、ローカルリポジトリの変更をプッシュします。
$ git push -u origin main
# ローカルリポジトリの変更を、origin として指定したリモートリポジトリにプッシュする
{% endmac %}
{% windows %}
-
{% data variables.product.product_location %}上で新しいリポジトリを作成します。 エラーを避けるため、新しいリポジトリはREADME、ライセンス、あるいは
gitignoreで初期化しないでください。 これらのファイルは、プロジェクトを {% data variables.product.product_name %}にプッシュした後で追加できます。
{% data reusables.command_line.open_the_multi_os_terminal %} -
ワーキングディレクトリをローカルプロジェクトに変更します。
-
Use the
initcommand to initialize the local directory as a Git repository. By default, the initial branch is calledmaster.If you’re using Git 2.28.0 or a later version, you can set the name of the default branch using
-b.$ git init -b mainIf you’re using Git 2.27.1 or an earlier version, you can set the name of the default branch using
&& git branch -m.$ git init && git branch -m main -
ファイルを新しいローカルリポジトリに追加します。 これで、それらのファイルが最初のコミットに備えてステージングされます。
$ git add .
# ローカルリポジトリにファイルを追加し、コミットに備えてステージします。 {% data reusables.git.unstage-codeblock %}
- ローカルリポジトリでステージングしたファイルをコミットします。
$ git commit -m "First commit"
# 追跡された変更をコミットし、リモートリポジトリへのプッシュに備えます。 {% data reusables.git.reset-head-to-previous-commit-codeblock %}
- At the top of your repository on {% ifversion ghae %}{% data variables.product.product_name %}{% else %}{% data variables.product.product_location %}{% endif %}'s Quick Setup page, click {% octicon "clippy" aria-label="The copy to clipboard icon" %} to copy the remote repository URL.

- コマンドプロンプトで、ローカルリポジトリのプッシュ先となるリモートリポジトリの URL を追加します。
$ git remote add origin <em> <REMOTE_URL> </em>
# 新しいリモートを設定する
$ git remote -v
# 新しいリモート URL を検証する
- {% data variables.product.product_location %} へ、ローカルリポジトリの変更をプッシュします。
$ git push origin main
# ローカルリポジトリの変更を、origin として指定したリモートリポジトリにプッシュする
{% endwindows %}
{% linux %}
-
{% data variables.product.product_location %}上で新しいリポジトリを作成します。 エラーを避けるため、新しいリポジトリはREADME、ライセンス、あるいは
gitignoreで初期化しないでください。 これらのファイルは、プロジェクトを {% data variables.product.product_name %}にプッシュした後で追加できます。
{% data reusables.command_line.open_the_multi_os_terminal %} -
ワーキングディレクトリをローカルプロジェクトに変更します。
-
Use the
initcommand to initialize the local directory as a Git repository. By default, the initial branch is calledmaster.If you’re using Git 2.28.0 or a later version, you can set the name of the default branch using
-b.$ git init -b mainIf you’re using Git 2.27.1 or an earlier version, you can set the name of the default branch using
&& git branch -m.$ git init && git branch -m main -
ファイルを新しいローカルリポジトリに追加します。 これで、それらのファイルが最初のコミットに備えてステージングされます。
$ git add .
# ローカルリポジトリにファイルを追加し、コミットに備えてステージします。 {% data reusables.git.unstage-codeblock %}
- ローカルリポジトリでステージングしたファイルをコミットします。
$ git commit -m "First commit"
# 追跡された変更をコミットし、リモートリポジトリへのプッシュに備えます。 {% data reusables.git.reset-head-to-previous-commit-codeblock %}
- At the top of your repository on {% ifversion ghae %}{% data variables.product.product_name %}{% else %}{% data variables.product.product_location %}{% endif %}'s Quick Setup page, click {% octicon "clippy" aria-label="The copy to clipboard icon" %} to copy the remote repository URL.

- ターミナルで、ローカルリポジトリがプッシュされるリモートリポジトリの URL を追加してください。
$ git remote add origin <em> <REMOTE_URL> </em>
# 新しいリモートを設定する
$ git remote -v
# 新しいリモート URL を検証する
- {% data variables.product.product_location %} へ、ローカルリポジトリの変更をプッシュします。
$ git push origin main
# ローカルリポジトリの変更を、origin として指定したリモートリポジトリにプッシュする
{% endlinux %}