カテゴリー
コンピューター

既存のローカル Git リポジトリに GitHub リモートリポジトリを追加する方法メモ

ポイント

  • GitHub でリポジトリを作成するときに、 README 、 .gitignore 、 LICENCE などファイルを作らない ようにする。
  • そうしないと、ローカルとリモートリポジトリの内容の差異によるエラー、ブランチ名の衝突によるエラーなどが発生して解決に手間がかかる。
  • エラー内容は、 すでに Git 管理しているローカルリポジトリを、新規で作った Github リポジトリに反映する方法 – oki2a24 参照
  • ローカルリポジトリとリモートリポジトリをつなぐためのコマンドは次。
    # ローカルリポジトリに、リモートリポジトリを origin という名前で追加
    git remote add origin https://github.com/oki2a24/vuejs_nyumon_75_todoapp.git
    # ローカルリポジトリの master ブランチと、リモートリポジトリ origin の master ブランチとを紐付け(ローカル master の上流ブランチとして origin 上の master ブランチを設定)
    git push -u origin master
    

具体的に行った手順

はじめに

ローカルで、学習用の Git リポジトリを作成しました。対象は次の本です。

後から見返せるように、そして記念に GitHub にアップしておこうと思いました。

GitHub リポジトリの作成

oki2a24/vuejs_nyumon_75_todoapp で作成しました。

  1. Create a New Repository
  2. Repository name 、 Description (optional) のみを入力し、 Public のままとした。 Initialize this repository with a README はチェックせず、 Add .gitignore: None 、 Add a licence: None 、とした。
  3. Create repository をクリックした。

ローカルリポジトリとリモートリポジトリをつなぐ

ローカルリポジトリで、次の作業を行いました。

実際のログです。

$ git status
On branch master
nothing to commit, working tree clean
$
$ git remote add origin https://github.com/oki2a24/vuejs_nyumon_75_todoapp.git
$
$ git push -u origin master
Enumerating objects: 52, done.
Counting objects: 100% (52/52), done.
Delta compression using up to 4 threads
Compressing objects: 100% (50/50), done.
Writing objects: 100% (52/52), 80.95 KiB | 2.79 MiB/s, done.
Total 52 (delta 18), reused 0 (delta 0)
remote: Resolving deltas: 100% (18/18), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/oki2a24/vuejs_nyumon_75_todoapp/pull/new/master
remote:
To https://github.com/oki2a24/vuejs_nyumon_75_todoapp.git
* [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
$

参考。 GitHub の Quick setup — if you’ve done this kind of thing before のコピー

GitHub で、ファイル一切無しでリポジトリを作成すると、今回行った手順を教えてくれるページが表示されました。そのコピーを載せておきます。

…or create a new repository on the command line

echo "# vuejs_nyumon_75_todoapp" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/oki2a24/vuejs_nyumon_75_todoapp.git
git push -u origin master

…or push an existing repository from the command line

今回使用したのはこちら。

git remote add origin https://github.com/oki2a24/vuejs_nyumon_75_todoapp.git
git push -u origin master

おわりに

今回の投稿は、次の投稿の改良版と言えます。

git push のオプションについては、次のページが参考になりました。ありがとうございます!

以上です。

コメントを残す