Quantcast
Viewing all articles
Browse latest Browse all 5712

#28/100DaysOfCode【gitでローカルリポジトリにコミットして、リモートリポジトリにプッシュしようとしてみた】

概要

本日やりたかったこと
①リモートリポジトリとローカルリポジトリの接続
②Eclipseで先週のフィードバックのリライト
③リモートへのプッシュ
④Javaの勉強へ

本日やったこと
①リモートリポジトリとローカルリポジトリの接続
②vimコマンドの理解
③add commit

できなかった理由~技術編~
①インデックスからローカルへコミットができていないままリモートへプッシュしようとしていた。
②untracked fileの対応。未追跡がどこで発生しているのかと、なぜ発生していると他のディレクトリがUPできないかがわからなかった。
③修正版のディレクトリをローカルリポジトリへアップするのができなかった(解決済み)
④リモートリポジトリへプッシュするはずが最後ごちゃごちゃってなった。(集中力が切れてなにがわからないかわからない、あした見る)

できなかった理由~その他ノウハウ~
①ターミナルの記録を残すときに注釈をいれながら理解していくこと。わからない部分がわからないまま飛んでしまいそうになる。

リモートリポジトリとローカルリポジトリの接続

昨日はローカルリポジトリの作成が完了したあと
リモートリポジトリとの接続ができずに終わった。

そのときのエラーメッセージをどうして控えておかなかったのか、、、不覚、、、

というわけでもう一度リモートリポジトリとローカルリポジトリを接続するところから始める。

昨日参考にしたサイトは下記。
【さるわか】

tomimotukenoMBP:~ yusuketomimoto$ git remote add origin https://github.com/ytomimoto/WEBbytom.git

まずはリモートリポジトリを接続して

tomimotukenoMBP:~ yusuketomimoto$ git push https://github.com/ytomimoto/WEBbytom.git gittest
error: src refspec gittest does not match any.

プッシュを実行。

そしてこのエラーメッセージである。
確か昨日の記憶によると
プッシュしてくれと指定したブランチの名前"gittest"が違うか
そもそもローカルにコミットできていないケースがある。

なのでまずローカルのブランチの名前を確認する。
【it branchメモ - ローカルやリモートのブランチ一覧を見る。リモートブランチ(追跡ブランチ)の情報を更新する(オプション-r、-a)】
この方法で確認できるらしい。

Image may be NSFW.
Clik here to view.
image.png

するとどうやらそもそもブランチがローカル上にない様子。昨日コミットしたはずだが、、、

というわけでもう一度ディレクトリ・ローカルリポジトリを作成しローカルへコミットしてみようと思う。
【今さら聞けない!GitHubの使い方【超初心者向け】】

Image may be NSFW.
Clik here to view.
image.png

Image may be NSFW.
Clik here to view.
image.png

ううう、やっとできた、、、やったぞ、、、
多分昨日インデックスからローカルへのコミットができていなかったのだろう。

Image may be NSFW.
Clik here to view.
image.png

やはり確認するとローカルでブランチが作成されている。
Image may be NSFW.
Clik here to view.
image.png

(色が薄くて見づらい、、、すいません、、、)

では昨日はどういう状態だったのだろう??
Image may be NSFW.
Clik here to view.
image.png

やっぱりコミットされた形跡がある。
ローカルにコミットできているはずなのにgit branch で確認できていない状況ってなんだろう。

もう一度やってみる。
まずワークツリー内で先程用意したhello.htmlを更新する。(new addを追加)

Image may be NSFW.
Clik here to view.
image.png

いまワークツリー内で更新をしたので一度
下記を参考にしてローカルリポジトリへコミットしてみたいと思う。

//

Image may be NSFW.
Clik here to view.
image.png

//

ファイルを変更するには一度git add→ git commitをする必要がありそうである。
するとそもそも指定したファイル名に一致するファイルがディレクトリに存在しないと表示されている。

tomimotukenoMBP:~ yusuketomimoto$ git add hello.html
fatal: pathspec 'hello.html' did not match any files
tomimotukenoMBP:~ yusuketomimoto$ git commit -m "add new file"
On branch master

念の為確認したがディレクトリにはやはり存在している。
Image may be NSFW.
Clik here to view.
image.png

下記の通りワークツリー内に存在しているはずなのになぜだろう。

Image may be NSFW.
Clik here to view.
image.png

今現状としては初回はリモートにプッシュできるものの、二回目以降プッシュができない状態である。

修正版をローカルリポジトリへコミット→リモートリポジトリにプッシュする方法を調べてみたいと思う。

//

Image may be NSFW.
Clik here to view.
image.png

//

これを見てみるとどうやらすでにインデックスに存在しているファイルに変更を加えてローカルリポジトリにコミットするのではなく、
インデックスに存在しているファイルを一度コピってそれを編集して別フアイルとしてインデックスにあげる必要があるのか?

tomimotukenoMBP:~ yusuketomimoto$ git add hello.html
fatal: pathspec 'hello.html' did not match any files
tomimotukenoMBP:~ yusuketomimoto$ git add .
warning: adding embedded git repository: Documents/GitHub/iceberg.vim
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint:   git submodule add <url> Documents/GitHub/iceberg.vim
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached Documents/GitHub/iceberg.vim
hint: 
hint: See "git help submodule" for more information.

やはり最初にgit add でワークツリーにある更新済みファイルをインデックスに
加えようとすると対象ファイルが見つからないとなってしまう。

もう一度addについて調べてみる必要がありそうだ。
ドットインストールがいいらしいので少しみてみる。

参考にしてコミットしてみたが、やはり新しいディレクトリの作成とローカルへの追加はできる。
入力画面から抜けられないと思ったらvimというエディターが起動していたらしい。
【vimの抜け方】

Image may be NSFW.
Clik here to view.
image.png

nothing added to commit but untracked files present
tomimotukenoMBP:~ yusuketomimoto$ git push origin master
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/ytomimoto/WEBbytom.git'
tomimotukenoMBP:~ yusuketomimoto$ 

この()内の違いはなんだ、、、→ git log --onelineでシンプルにコミットを見ることが可能。

ドットインストールを進めていくと6コマ目で
既にインデックスへ上がっているディレクトリを更新してローカルへプッシュする方法が上がっていた。

tomimotukenoMBP:awesome yusuketomimoto$ git log --stat
///--stat オプションは、各コミットエントリに続けて変更されたファイルの一覧と変更されたファイルの数、追加・削除された行数が表示される。
commit 4fdce660f0c4638a01663dbf8e737e6d7ca7d719 (HEAD -> master)
Author: ytomimoto <interntomimoto0104@gmail.com>
Date:   Sun Sep 9 17:18:51 2018 +0900

    initial commit
    :wq
    :wq
    :wc:wq

 hello2.html | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

commit 2837f9268a2251bc1abcc141e21c15e156f554f6 (origin/master)
Author: ytomimoto <interntomimoto0104@gmail.com>
Date:   Sun Sep 9 10:51:20 2018 +0900

    add new file

 hello.html | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tomimotukenoMBP:awesome yusuketomimoto$ ls
hello.html  hello2.html

//今回はvim上で修正を加えているが通常はIDE上からインデックス内のファイルへ変更を加えることになると思う。

tomimotukenoMBP:awesome yusuketomimoto$ vim hello.html
tomimotukenoMBP:awesome yusuketomimoto$ git status
On branch master

//ここではインデックスにもステージされてないし、ローカルにもコミットされていないという状態を指している。
Changes not staged for commit:

//インデックスに載せたいならadd
  (use "git add <file>..." to update what will be committed)

//変更が間違っているならcheckoutをしなさいよとなっている
  (use "git checkout -- <file>..." to discard changes in working directory)

//このmodifiedが変更されましたという意味になっている。

    modified:   hello.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .index.html.swp

no changes added to commit (use "git add" and/or "git commit -a")
tomimotukenoMBP:awesome yusuketomimoto$ 

//ここで一旦間違えた設定で修正をなくしてみる
tomimotukenoMBP:awesome yusuketomimoto$ git checkout -- hello.html

//catは泥棒ネコ的な意味合いで覚える。盗み見る。
tomimotukenoMBP:awesome yusuketomimoto$ cat hello.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="1561.6">
  <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
  </style>
</head>
<body>
<p class="p1">Test</p>
</body>
</html>
tomimotukenoMBP:awesome yusuketomimoto$ 

ここまでで既にインデックス上にある
ディレクトリの修正が完了した。

tomimotukenoMBP:awesome yusuketomimoto$ git status
On branch master


//このアントラックドファイルの意味が不明、あとからググろう。
Untracked files:

  (use "git add <file>..." to include in what will be committed)

    .index.html.swp

nothing added to commit but untracked files present (use "git add" to track)

//とりあえず今修正したファイルをインデックスにステージしようと思う。

tomimotukenoMBP:awesome yusuketomimoto$ git add hello.html

//ここまではおけ
tomimotukenoMBP:awesome yusuketomimoto$ git status
On branch master

//するとやはりアントラックドファイルが登場。
//ドットインストールでは 新しくコミットしましたを意味する英文が出ているのになぜ、、、
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .index.html.swp

nothing added to commit but untracked files present (use "git add" to track)

というわけでUntracked filesとの格闘スタート。
【.2 Git の基本 - 変更内容のリポジトリへの記録】
//
作業コピー内の各ファイルには 追跡されている(tracked) ものと 追跡されてない(untracked) ものの二通りがあることを知っておきましょう。追跡されている ファイルとは、直近のスナップショットに存在したファイルのことです。これらのファイルについては変更されていない(unmodified)」「変更されている(modified)」「ステージされている(staged)」の三つの状態があります。追跡されていないファイルは、そのどれでもありません。直近のスナップショットには存在せず、ステージングエリアにも存在しないファイルのことです。最初にプロジェクトをクローンした時点では、すべてのファイルは「追跡されている」かつ「変更されていない」wq状態となります。チェックアウトしただけで何も編集していない状態だからです。

ファイルを編集すると、Git はそれを「変更された」とみなします。直近のコミットの後で変更が加えられたからです。変更されたファイルを ステージ し、それをコミットする。この繰り返しです。ここまでの流れを図 2-1 にまとめました。
//

スナップショットって?なんなん?
//
スナップショットとは、ある時点でのソースコードや、ファイル、ディレクトリ、データベースファイルなどの状態を抜き出したもののことです。 バックアップをする際に、バックアップ処理中にファイルが更新されると、ファイルやディレクトリに不整合が起こることがあります。 これを防ぐために使用されるのがスナップショットです。
//

今回はuntracked file については未追跡というとこまでの理解でとどめておく。
どこで未追跡ファイルと認識されるのかについては知っておかないといけない。
またなんで未追跡ファイルがあると他のファイルを add できないのかも気になる。

とりあえず下記で削除の方法だけ確認しておく。
【git Untracked files(未追跡ファイル)を1発で消し去る】

tomimotukenoMBP:awesome yusuketomimoto$ git clean -f
Removing .index.html.swp
tomimotukenoMBP:awesome yusuketomimoto$ git status
On branch master
nothing to commit, working tree clean
tomimotukenoMBP:awesome yusuketomimoto$ git add hello.html
tomimotukenoMBP:awesome yusuketomimoto$ git status
On branch master
nothing to commit, working tree clean
tomimotukenoMBP:awesome yusuketomimoto$ vim hello.html
tomimotukenoMBP:awesome yusuketomimoto$ git add hello.html
tomimotukenoMBP:awesome yusuketomimoto$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   hello.html

tomimotukenoMBP:awesome yusuketomimoto$ 

これで修正したものをローカルリポジトリへコミット完了。
そして最後にこれをリモートへプッシュする。

tomimotukenoMBP:awesome yusuketomimoto$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 681 bytes | 681.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/ytomimoto/WEBbytom.git
   2837f92..4fdce66  master -> master

できたっぽいのでは??
しかし

tomimotukenoMBP:awesome yusuketomimoto$ cat hello.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title>aaaaaaaaaaaaaaaaaaaaaaaaaa</title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="1561.6">
  <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
  </style>
</head>
<body>
<p class="p1">Test</p>
</body>
</html>
tomimotukenoMBP:awesome yusuketomimoto$ 

ローカル上ではタイトルにa連打している。
statusでも

tomimotukenoMBP:awesome yusuketomimoto$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   hello.html

になっているにもかかわらずリモート上では更新されていない。
謎である。

Image may be NSFW.
Clik here to view.
image.png

てか向こうで更新扱いになってないし、、、

Image may be NSFW.
Clik here to view.
image.png

2分前にリモートにプッシュしたはずなのだが、、、

tomimotukenoMBP:awesome yusuketomimoto$ git remote add hello.html git@github.com:ytomimoto/WEBbytom.git
tomimotukenoMBP:awesome yusuketomimoto$ git push hello.html git@github.com:ytomimoto/WEBbytom.git
error: src refspec git@github.com does not match any.
error: failed to push some refs to 'git@github.com:ytomimoto/WEBbytom.git'
tomimotukenoMBP:awesome yusuketomimoto$ git push git@github.com:ytomimoto/WEBbytom.git hello.html
error: src refspec hello.html does not match any.
error: failed to push some refs to 'git@github.com:ytomimoto/WEBbytom.git'
tomimotukenoMBP:awesome yusuketomimoto$ git push git@github.com:ytomimoto/WEBbytom.git master
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
Enter passphrase for key '/Users/yusuketomimoto/.ssh/id_rsa': 
Enter passphrase for key '/Users/yusuketomimoto/.ssh/id_rsa': 
Everything up-to-date
tomimotukenoMBP:awesome yusuketomimoto$ 

やば、なにかしたかな、、、

とりあえずリモートへプッシュができない汗
けど今日はvimの簡単な使い方とadd commitの使い方がわかった。


Viewing all articles
Browse latest Browse all 5712

Trending Articles