71
71

More than 5 years have passed since last update.

【Github】ローカルからリモートリポジトリのへプッシュ

Last updated at Posted at 2017-11-27

プロジェクト等をローカルからリモートへプッシュする際の備忘録です。

1. 想定されるケース

ローカル作成したプロジェクトをGithubのリモートリポジトリへプッシュする。

2. Terminalで該当プロジェクトまで移動

cd
cd Desktop/
cd yourProjectName/

3. 該当プロジェクト内容確認

下記のコマンドでプロジェクトフォルダ内でgit管理下に置かれていないファイルが確認できる。

git init // ローカルプロジェクトのイニシャライズ
git status

4. まずローカル上でgit管理下に置く

git add .
git commit -m "First commit."

git add .は、新規作成・変更されたファイルのみをaddする。

念のためbranchを確認(defaltはmaster)

git branch
// * master と返ってくる

5. リモートリポジトリへ接続・プッシュ

Github上でリモートリポジトリを作成し、Quick StartのURLを入手。
下記のコマンドを入力して、ローカルとリモートを接続。
※この際、「READ.MEを自動作成する」的な項目にチェックを入れると、Quick StartのURLが出てこない。

sshキーを連動させている場合は、push前の段階でパスワード入力画面に。

git remote add origin ssh://git@github.com:UserName/ProjectName
git push -u origin master

プッシュまで完了したら、Githubの対象リポジトリの内容をブラウザ更新して確認。
正常にフォルダがアップロードされていたら終了。

エラーが返ってくる場合の対処法

fatal: remote origin already exists.

pushしようとした際に、上記のようなエラーメッセージが返ってくる場合があります。
その際は、"git remote rm origin"で一度originを削除し、再度originを登録することで正常にpushできます。

git remote rm origin
git remote add origin git@github.com:UserName/ProjectName
git push -u origin master

error: failed to push some refs to 'git@github.com:---.git'

初回以降にcommitした内容をpushしようとする際、このようなエラーメッセージが返ってくる場合があります。

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

これは、リモートにプッシュする前にリモートが他のpush等によって変更されたために起こるエラー。
"git pull origin master"でリモートの変更をpullすることによって解決します。

git pull origin master

上記のコードを入力するとVimの画面になるので、"commit -m"のようにメッセージを登録することで、正常にpushできるようになります。

※Vim操作
i = 文字入力(insert)
wsc = Vim操作(insert)終了
:wq = 入力完了(Write and Quit)

ssh: Could not resolve hostname UserName: nodename nor servname provided, or not known

リモートレポジトリに接続して、初回のpushをしようとしたところ、このようなエラーが返ってくることがあります。

ssh: Could not resolve hostname UserName: nodename nor servname provided, or not known

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

色々ググって結局原因はわからないままなのですが、先ほどのエラーと同様に一度originを削除したところ、正常にpushされました。

git remote rm origin
git remote add origin git@github.com:UserName/ProjectName
git push -u origin master

以上です。

71
71
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
71
71