Git(基本操作)

git

gitでの基本操作を学習しよう。

はじめてのコミット

[フォルダ作成]
作業フォルダを作成しよう。今回はdesktopにgit_basicフォルダを作成し、そこに移動する。

 
$ mkdir ~/desktop/git_basic
$ cd ~/desktop/git_basic
 

一番最初にやることはgit initだ。これでgitの管理下に置かれる

なにかしたらすぐにgit statusで確認するクセをつけるとよいだろう

今はまだcommitオブジェクトが存在していないので
nothing to commit
と表示される。

さっそくファイルを1枚作ってみよう。今回はtest.txtを作成する。

vimが開くので以下のように1行入力して:wqする。

git statusしてみよう。

$ git status
On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
  test.txt
nothing added to commit but untracked files present (use "git add" to track)

作業フォルダの変更を検知してメッセージが表示されている。
test.txt
という知らないファイルが存在している。
というメッセージだ。

gitで履歴を作っていくにはまずステージングエリアと呼ばれるところに変更したい情報をaddしなければならない。

さっそく今作ったtest.txtをaddしよう。

なにかしたらgit status

$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
  new file:   test.txt

test.txtファイルがステージングエリアにあがってコミットできる状態になっている。コミットしてみよう。

1$ git commit -m"first commit"
2 
3[master (root-commit) 27fae62] first commit
4 1 file changed, 1 insertion(+)
5 create mode 100644 test.txt

gitはコミットする際に必ずどういう変更をしたのかということを記すコミットメッセージというのを付与しなければならない。
git commit -m
と-mオプションをつけてコミットすることでcommitと同時にメッセージを付与することができる。今回は”first commit”というコミットメッセージを付与した。

コミットオブジェクトができたのでgit graphしてみよう。
(git graph はgit log –graphを短いコマンドで実行できるようにgit講座の初回に設定してある)

1 
2$ git graph
3* 27fae62  (HEAD -> master) 2020-01-21 mjpurin first commit
4 

左に27fae62というこのコミットオブジェクトのハッシュ値が表示されている。
このハッシュ値はコミット識別するのに使われる。

2度目のコミット

test.txtを変更しよう。viにて2行目を追加して以下の状態にする。

git statusで確認

1$ git status
2 
3Changes not staged for commit:
4  (use "git add <file>..." to update what will be committed)
5  (use "git restore <file>..." to discard changes in working directory)
6  modified:   test.txt

作業フォルダに変更があったことをgitが感知している。今回は差分を確認してみよう。

$ git diff
 
diff --git a/test.txt b/test.txt
index c380edd..efac604 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 first_line
+second_line

この状態でのdiffコマンドは最初のコミットと現在の作業フォルダの差分を表示してくれている。(+記号は新しく加わった行を意味している。)

変更内容も確認したのでaddしよう。

先程はgit addの後ろにファイル名を書いたが.記号を使うとそのフォルダで行われた変更全部をステージングエリアに上げることを意味する。とても便利なコマンドなので多用される。statusをみよう。

1$ git status
2 
3On branch master
4Changes to be committed:
5  (use "git restore --staged <file>..." to unstage)
6  modified:   test.txt

test.txtの変更を含んだコミットを作成できる状態という表示されている。
ここで先程のgit diffをしてみよう。

しかし、何も表示されない。addしたあとのdiffコマンドはステージングエリアと作業フォルダの差分を表示する。最新コミットとの変更を知りたい場合は以下のように入力する。

HEADというのは今作業しているブランチの最新のコミットオブジェクトを指すポインターでGitではかなり重要な意味を持つ言葉だ。

差分も確認したのでcommitしよう。

コミットしたらgit graphだ。

1$ git graph
2 
3* 5cc2965  (HEAD -> master) 2020-01-21 mjpurin second commit
4* 27fae62  2020-01-21 mjpurin first commit

2つ目のコミットオブジェクトが作成された。

同じようにtest.txtにthird_lineを追記して3つ目のコミットオブジェクトを作成してみてもらいたい。(実際に自分で作業する)

できたであろうか?git graphすると以下のような感じなるはずだ。

$ git graph
 
* 266f605  (HEAD -> master) 2020-01-21 mjpurin third commit
* 5cc2965  2020-01-21 mjpurin second commit
* 27fae62  2020-01-21 mjpurin first commit

昔に戻りたい・・・

あのときに戻りたい。。。実際の人生ではできないがgitを使っていれば簡単に戻れる。まずは完全にそのときに状態にもどす方法をやろう。

作業フォルダも履歴も完全に1時間前に戻った。完全にリセットしてやり直したい場合にはよい方法だ。

もちろん復元する手段も提供されている。以下のように入力

1 
2$ git reflog
3 
427fae62 (HEAD -> master) HEAD@{0}: reset: moving to 27fae
5266f605 HEAD@{1}: commit: third commit
65cc2965 HEAD@{2}: commit: second commit
727fae62 (HEAD -> master) HEAD@{3}: commit (initial): first commit
8 

reflogはリファレンスログで過去に行ったコミットオブジェクトを参照できる。
third commitしたオブジェクト(266f)に再びreset –hardして復元しよう。

$ git reset --hard 266f
HEAD is now at 266f605 third commit

git graphで確認

1$ git graph
2 
3* 266f605  (HEAD -> master) 2020-01-21 mjpurin third commit
4* 5cc2965  2020-01-21 mjpurin second commit
5* 27fae62  2020-01-21 mjpurin first commit

third commitをした直後の状態に復元された。

続きはこちら

git
スポンサーリンク
シェアする
mjpurinをフォローする

コメント

タイトルとURLをコピーしました