0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Githubの使い方 その3 (PUSHについて)

Last updated at Posted at 2024-01-21

前回までの流れ GitHub使い方

  1. 今いるディレクトリにディレクトリを作成するmkdir git_tutorial
  2. 作成したディレクトリに移動するcd git_tutorial
  3. git管理をはじめるgit init
  4. 作成したディレクトリに移動cd ディレクトリ名
  5. ファイルを作成 touch ファイル名.拡張子  echo "記述したい内容" >> ファイル名.拡張子など
  6. ファイルをステージに追加する
    git add ファイル名.拡張子 または、git add .
  7. 変更状況を確認するgit status git diff
  8. ローカルリポジトリにコミット git commit -m "コメント"  

3. リモートリポジトリにPUSHする

※GithubへのSSH接続は済ませてあります。

3-1 SSH接続が可能か確認

ssh -T git@github.com
  • 以下のようにパスワードの入力が求められる。入力しても画面には表示されない仕組み。
    スクリーンショット 2024-01-21 9.03.38.png

  • 初めて接続する際は以下のように聞かれるのでyesで。
    Warning: Permanently added 'github.com,13.114.40.48' (RSA) to the list of known hosts. 2Enter passphrase for key '/home/ec2-user/.ssh/id_rsa':

  • 以下のようなコメントが出たらSSH接続できています。
    スクリーンショット 2024-01-21 9.01.02.png

3-2. GitHub 上にリモートリポジトリを作成

Test-repositoryという名前でリポジトリーを作成します。

  • Github上で、New repositoryの順に押下
    スクリーンショット 2024-01-21 14.27.02.png
  • リポジトリーに任意の名前をつけて、公開・非公開を選択。最後に緑色のCreate repository を押下。
    スクリーンショット 2024-01-21 14.30.18.png
  • 作成したリポジトリーのURLが生成されるのでSSHの方をコピーしておく。
    スクリーンショット 2024-01-21 14.34.46.png

3-3. リモートリポジトリにPUSH

*事前にhelloworldという名前のローカルリポジトリにhello.txtというファイルをaddしてあります。

  • リモートリポジトリとして先程作成した GitHub上のリポジトリを指定する。
git remote add origin <GitHubのリモートリポジトリのURL>

実際にやってみたら。。。エラー。
スクリーンショット 2024-01-21 15.07.32.png

(エラー内容の拡大error: remote origin already exists.)
これは既にリモートリポジトリが設定されているというエラー。このブログを書く前に試しに別のリモートリポジトリーに接続していたので、そちらに繋がったままだよの意味。
なので、下記のコマンドで解決。

git remote remove origin
git remote add origin 新しいリモートリポジトリのURL

git remote -vで現在設定されてるリポートリポジトリの確認。
スクリーンショット 2024-01-21 15.06.04.png

無事、Test-repositoryに設定できた。

  • ローカルリポジトリの内容をリモートリポジトリにPUSH。-uオプションは、以降の git push git pull コマンドを実行する際に、ブランチ名を省略して実行できるようになる。
git push -u origin main

スクリーンショット 2024-01-21 15.04.51.png
以下の一文は、
リモートリポジトリ(origin)にあるデフォルトブランチ(main)と、ローカルリポジトリ(git_test)のデフォルトブランチ(main)が紐づいたことを表している。
スクリーンショット 2024-01-26 21.56.20.png

  • Githubに移動して〜 確認!よし、出来てます!
    スクリーンショット 2024-01-21 15.09.53.png
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?