開発者ドキュメント

既存のプロジェクトをGitHubにプッシュする方法

Introduction

GitHub is a cloud-hosted Git management tool. Git is distributed version control, meaning the entire repository and history lives wherever you put it. People tend to use GitHub in their business or development workflow as a managed hosting solution for backups of their repositories. GitHubは、同僚、友人、組織などとつながることで、これをさらに進めます。

In this tutorial, you will learn how to take an existing project you are working on and push it, so it also exists on GitHub.

Prerequisites

リポジトリを初期化してGitHubにプッシュするには、次のものが必要です。

  1. 無料のGitHubアカウント
  2. git ローカルマシンにインストールされている

Step 1 — Create a new GitHub Repo

Sign in to GitHub and create a new empty repo. READMEを初期化するかどうかを選択できます。 とにかくこのリモートリポジトリ内のすべてをオーバーライドするだけなので、実際には問題ではありません。

Warning: Through the rest of this tutorial, we’ll assume your GitHub username is sammy 作成したリポジトリの名前は my-new-project. It is important that you replace these placeholders with your actual username and repo name.

Step 2 — Initialize Git in the project folder

From your terminal, run the following commands after navigating to the folder you would like to add.

Gitリポジトリを初期化する

GitHubにプッシュして実行するプロジェクトのルートディレクトリにいることを確認してください。

Note: If you already have an initialized Git repository, you can skip this command.

  1. git init

このステップは隠されたものを作成します .git directory in your project folder, which the git ソフトウェアは、プロジェクトのすべてのメタデータとバージョン履歴を認識して保存するために使用します。

ファイルをGitインデックスに追加します

  1. git add -A

The git add コマンドは、コミットに含めるファイルをgitに指示するために使用されます。 -A (or --all) argument means “include all”.

追加されたファイルをコミットする

  1. git commit -m 'Added my project'

The git commit コマンドは、「追加」されたすべてのファイルで新しいコミットを作成します。 The -m (or --message) sets the message that will be included alongside the commit, used for future reference to understand the commit. In this case, the message is: 'Added my project'.

Add a new remote origin

  1. git remote add origin git@github.com:sammy/my-new-project.git

Note: Remember, you will need to replace the highlighted parts of the username and repo name with your own username and repo name.

In git, a “remote” refers to a remote version of the same repository, which is typically on a server somewhere (in this case, GitHub). “origin” is the default name git gives to a remote server (you can have multiple remotes) so git remote add origin このリポジトリのデフォルトのリモートサーバーのURLを追加するようにgitに指示しています。

GitHubにプッシュ

  1. git push -u -f origin main

The -u (or --set-upstream) flag sets the remote origin as the upstream reference. This allows you to later perform git pushgit pull commands without having to specify an origin since we always want GitHub in this case.

The -f (or --force) flag stands for force. これにより、リモートディレクトリ内のすべてが自動的に上書きされます。 We’re using it here to overwrite the default README that GitHub automatically initialized.

Note: If you did not include the default README when creating the project on GitHub, the -f フラグは実際には必要ありません。

すべて一緒に

  1. git init
  2. git add -A
  3. git commit -m 'Added my project'
  4. git remote add origin git@github.com:sammy/my-new-project.git
  5. git push -u -f origin main

結論

これで、GitHubでコードの変更をリモートで追跡する準備が整いました。 As a next step, here’s a complete guide to how to use git.

プロジェクトで他の人との共同作業を開始したら、プルリクエストを作成する方法を知りたいと思うでしょう。

モバイルバージョンを終了