はじめに
先日、メインのMacBookがクラッシュしたのでPCを変更するハメになりました
そこでGitHubのリモートリポジトリからデータを引っ張ってきて開発を再開するも、
git push
の段階でrejected
が出て詰まってしまったので解決まで対処した流れを共有します。
この記事が役に立つ方
- メイン端末と別の端末から開発を進めようとしたらうまく
git push
出来なくて詰まっている方
この記事のメリット
- 急に端末がクラッシュしても(GitHubにデータがあれば)開発が再開出来る
環境
- macOS Mojave バージョン10.14.6
- シェル:zsh
- Git:以下参照
❯ git --version
git version 2.21.0 (Apple Git-122.2)
対処した流れ
1.リモートリポジトリのデータをダウンロード
まずはデータがないと始まりません。
自分のGitHubにあるデータを「Clone or Download」から「Download zip」でローカルにダウンロードし、所定のディレクトリに保存しました。
![スクリーンショット 2019-11-20 1.24.49.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F483292%2F1f9c731b-f468-cacc-e5d8-935fd5b8c5ed.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c75689da4e536a22ac644ad326f10e63)
![スクリーンショット 2019-11-20 1.25.12.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F483292%2Ff391154d-931b-39fd-8735-b61923afcd7f.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=e300de83f2972eae6f1034e1b85f7496)
今思うとそもそもここは
git clone リモートリポジトリのURL
で引っ張っても良かったように思います。
2.git init
❯ git init
Initialized empty Git repository in /Users/作業ディレクトリ/.git/
3. ローカルで作業
いくつかファイルをいじり、キリがついたのでgit push
までしようと考えました。
4.git add -A
❯ git add -A
add
は完了。
5. git commit
❯ git commit -m "コミット名"
変更箇所が羅列される
commit
も無事完了です。
6. 'git push'
❯ git push リモートリポジトリ名 master
fatal: 'リモートリポジトリ名' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
ここで「そんなリモートリポジトリなんてないよ!」と怒られました。
そういえば端末が変わっていたのでgit remote add
でリモートリポジトリを指定しないといけないことを思い出します。
7. git remote add
❯ git remote add リモートリポジトリ名 リモートリポジトリのURL
リモートリポジトリのURLはGitHubから確認可能です。
これで無事リモートリポジトリの指定は完了しました。
8.2度目のgit push
❯ git push リモートリポジトリ名 master
To リモートリポジトリのURL
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'リモートリポジトリのURL'
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.
「リモートにローカルにはない変更が含まれているので、更新できません!」と怒られます。
あれ?でもリモートから引っ張ってきたのになんで?と疑問発生。
9.git push -f
❯ git push -f リモートリポジトリ名 master
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
最新はローカルで間違いはなかったので、-f
オプションで強制的にpush
しようとするも、次は「権限がありません!」と怒られました。
そういえば公開鍵の作成とデプロイがまだ済んでいないことに気付きます。
10.ssh-keygen
で公開鍵を作成
❯ ssh-keygen -t rsa -C "メールアドレス"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/PCユーザー名/.ssh/id_rsa): /Users/PCユーザー名/.ssh/任意のファイル名
Enter passphrase (empty for no passphrase): ※パスフレーズを決めて入力(推奨。入力なしでEnterでも可。)
Enter same passphrase again: ※同じものを入力
Your identification has been saved in /Users/PCユーザー名/.ssh/任意のファイル名.
Your public key has been saved in /Users/PCユーザー名/.ssh/任意のファイル名.pub.
The key fingerprint is:
フィンガープリント
メールアドレス
The key's randomart image is:
+---[RSA 2048]----+
ランダムアートが表示される
+----[SHA256]-----+
11.pbcopy < ~/.ssh/任意のファイル名.pub
❯ pbcopy < ~/.ssh/任意のファイル名.pub
次のGitHubに公開鍵をデプロイする作業に向けて、さっき作成した公開鍵をpbcopy
でクリップボードにコピーします。
12. GitHubに公開鍵をデプロイ
GitHub上での作業となります。
下記手順です。
![スクリーンショット 2019-11-20 2.14.22.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F483292%2F916b909e-b348-68f7-d3a5-9d8542521498.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=5e476dd9b2e6a591206e81b2114fd5da)
![スクリーンショット 2019-11-20 2.14.31.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F483292%2F76c72873-9a59-aeda-14b2-03d64b720f9b.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=629f3a3cfe49527bc1154ab8d8264bbb)
![スクリーンショット 2019-11-20 2.14.47.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F483292%2Ff7658ef5-aa6b-c20d-a016-da49c67be59d.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=980c510b3a61823d363a79f43995b6e6)
![スクリーンショット 2019-11-20 2.14.53.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F483292%2F2ddb3947-acb8-dd7f-6792-81ec0610b3bb.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=80f67f377e77b6d82890637fb2481527)
![スクリーンショット 2019-11-20 2.16.19.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F483292%2F286838d9-136c-5147-9c04-7c1fa1e37a7d.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=1623055de8717d11148b7d0d63164bf6)
「Add key」を押して公開鍵のデプロイは完了です。
13.2度目のgit push -f
❯ git push -f リモートリポジトリ名 master
Enumerating objects: 166, done.
Counting objects: 100% (166/166), done.
Delta compression using up to 8 threads
Compressing objects: 100% (151/151), done.
Writing objects: 100% (166/166), 159.09 KiB | 1.05 MiB/s, done.
Total 166 (delta 15), reused 0 (delta 0)
remote: Resolving deltas: 100% (15/15), done.
To リモートリポジトリURL
+ XXXXXX...XXXXXX master -> master (forced update)
無事push
完了!
これ以降、通常通りGit管理が出来る状態となりました
おわりに
無事に復旧出来たので一安心!
ムダも多いかと思いますが、この手順を残したことがどなたかの参考になれば幸いです
今回、MacBookが急にシャットダウンを繰り返す現象が発生し、ハード面の問題のようでバックアップもとれず修理に出したのですが、ちゃんとGitHubに最新データを上げておいて良かった。
命拾いしました
参考にした記事
稚拙な記事ですが、自分の書いたものを見ながら解決しました。
【GitHub】15分調べても分からない人のためのSSH接続セットアップ方法 - Qiita