神戸学院大学 経営学部 林坂ゼミ

Git 入門トップページ

« 戻る 次へ »

Git によるバージョン管理入門 (Windows)

はじめてのバージョン管理

ここでは,簡単なバージョン管理を行ってみて,バージョン管理がどのようなものなのかを考えよう.

目次に戻る

ここでのストーリー

ここでは,「myReport」というプロジェクトに,「chapter1.txt」と「chapter2.txt」という2種類のテキストファイルを作成して,ファイルのバージョンを管理してみましょう.まず,「chapter1.txt」に数行の文章を作成し,Git にコミットします(つまり,バージョン管理システムに登録します).その後,「chapter2.txt」にも数行の文章を作成し,コミットします.さらに,「chapter1.txt」や「chapter2.txt」を編集してコミットしたり,編集を取り消して以前の状態に戻ったりします.

目次に戻る

myReportプロジェクトを作成する

Git では1個のフォルダ(ディレクトリ)を作成し,そのフォルダ内のファイル群を1つのプロジェクトとして管理することになります.ここでは,myReport という名前のフォルダを作成し,myReport 内のファイルを Git で管理することにします.まず,任意のフォルダ(今回は Documents フォルダ)に「myReport」フォルダを作成します.その後,そのフォルダに移動して,git init コマンドを実行します.これでプロジェクトを作成(初期化)できます.なお,git init でプロジェクトを作成すると,「.git」という名称の隠しフォルダが作成されます.

C:\Users\student>cd Documents ⏎ # Documents フォルダに移動する

C:\Users\student\Documents>mkdir myReport ⏎ # myReport フォルダを作成する

C:\Users\student\Documents>cd myReport ⏎ # myReport フォルダに移動する

C:\Users\student\Documents\myReport>git init ⏎ # git のプロジェクトを作成(初期化)する
Initialized empty Git repository in C:/Users/student/Documents/myReport/.git/

C:\Users\student\Documents\myReport>dir ⏎ # ファイルの確認
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 9807-E930 です

 C:\Users\student\Documents\myReport のディレクトリ

2024/03/24  14:13    <DIR>          .
2024/03/24  14:13    <DIR>          ..
               0 個のファイル                   0 バイト
               2 個のディレクトリ  58,337,857,536 バイトの空き領域

C:\Users\student\Documents\myReport>dir /a:h ⏎ # 隠しファイル(フォルダ)の確認
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 9807-E930 です

 C:\Users\student\Documents\myReport のディレクトリ

2024/03/24  14:13    <DIR>          .git
               0 個のファイル                   0 バイト
               1 個のディレクトリ  58,337,857,536 バイトの空き領域

C:\Users\student\Documents\myReport>

なお,Visual Studio Code を利用する場合は,コマンドプロンプトでプロジェクトのフォルダまで移動した状態で code . コマンドを実行すると Visual Studio Code が起動し,そのフォルダを開くことができます.

C:\Users\student\Documents\myReport>code . ⏎

C:\Users\student\Documents\myReport>

目次に戻る

chapter1.txtを作成する

まずは,myReport フォルダに「chapter1.txt」ファイルを作成します.任意のテキストエディタ (Visual Studio Code, 秀丸エディタ,メモ帳など) を使って次のようなテキストファイルを作成するとよいでしょう.なお,Windows 上の秀丸エディタやメモ帳を利用する場合には「文字コード」に「Unicode (UTF-8)」を指定して保存するようにしてください.

chapter1.txtGitのお勉強
神戸学院大学
林坂ゼミ

また,Visual Studio Code を利用している場合には,「Files: Trim Trailing Whitespace, Insert Final Newline, Trim Final Newlines」という3つの設定をオンにしておくことをお勧めします.これによって,行末尾の空白やファイル最後の空行が自動的にトリミングされ,最後の行には改行文字が確実に1個だけ残るようるので,Git でバージョンを管理するときに無駄なコミット履歴が残らなくなくなります.

目次に戻る

コマンドプロンプトの文字コードを変更する

まずはコマンドプロンプトを使って,今作成した chapter1.txt の中身を閲覧してみよう.このとき,Windows の環境によっては「Unicode (UTF-8)」で保存したファイルはコマンドプロンプトで文字化けしてしまうことがあります.このような場合はchcp 65001 コマンドによって,コマンドプロンプトの文字コードを変更する必要があります.

C:\Users\student\Documents\myReport>dir ⏎ # ファイルの確認

 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 9807-E930 です

 C:\Users\student\Documents\myReport のディレクトリ

2024/03/24  14:16    <DIR>          .
2024/03/24  14:16    <DIR>          ..
2024/03/24  14:17                51 chapter1.txt
               1 個のファイル                  51 バイト
               2 個のディレクトリ  58,321,248,256 バイトの空き領域

C:\Users\student\Documents\myReport>type chapter1.txt ⏎ # ファイルの中身を閲覧する(文字化け)

Git縺ョ縺雁級蠑キ
逾樊虻蟄ヲ髯「螟ァ蟄ヲ
譫怜揩繧シ繝・

C:\Users\student\Documents\myReport>chcp ⏎ # コマンドプロンプトの文字コードを確認

現在のコード ページ: 932

C:\Users\student\Documents\myReport>chcp 65001 ⏎ # 文字コードを変更


Active code page: 65001

C:\Users\student\Documents\myReport>dir ⏎ # ファイルの確認 # ファイルの中身を確認

 Volume in drive C has no label.
 Volume Serial Number is 9807-E930

 Directory of C:\Users\student\Documents\myReport

2024/03/24  14:16    <DIR>          .
2024/03/24  14:16    <DIR>          ..
2024/03/24  14:17                51 chapter1.txt
               1 File(s)             51 bytes
               2 Dir(s)  58,320,879,616 bytes free

C:\Users\student\Documents\myReport>type chapter1.txt ⏎

Gitのお勉強
神戸学院大学
林坂ゼミ

C:\Users\student\Documents\myReport>

目次に戻る

コミットしてみよう

保存ができれば,実際にコミット(登録)してみよう.Git ではプロジェクトフォルダに保存されたファイルがすべて管理対象に含まれる(.gitignore ファイルを使って指定したファイルを管理対象から除くことも可能です).管理対象のファイルで変更されたファイルを確認し,コミットしたいファイルは git add ファイル名 コマンドによって「ステージングエリア」に登録します.その後,git commit -m"メッセージ" によってコミットします.

C:\Users\student\Documents\myReport>dir ⏎ # ファイルの確認
 Volume in drive C has no label.
 Volume Serial Number is 9807-E930

 Directory of C:\Users\student\Documents\myReport

2024/03/24  14:16    <DIR>          .
2024/03/24  14:16    <DIR>          ..
2024/03/24  14:17                51 chapter1.txt
               1 File(s)             51 bytes
               2 Dir(s)  58,320,478,208 bytes free

C:\Users\student\Documents\myReport>git status ⏎ # トラックされていない(新しいファイル)がある
On branch main

No commits yet

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

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

C:\Users\student\Documents\myReport>git add chapter1.txt ⏎ # ファイルをステージングエリアにアップする

C:\Users\student\Documents\myReport>git status ⏎ # コミットの準備ができていることを確認する
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   chapter1.txt


C:\Users\student\Documents\myReport>git commit -m"initial commit" ⏎ # コミットしてみる
[main (root-commit) ce2533a] initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 chapter1.txt

C:\Users\student\Documents\myReport>git log ⏎ # コミットログを確認する
commit ce2533a511d5f110eb5cc04f4c7571a49d8d7846 (HEAD -> main)
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:20:07 2024 +0900

    initial commit

C:\Users\student\Documents\myReport>

このコミット作業によって,この時点のファイルの状態が保存されるので,将来的に必要になればこの時点の状態にいつでも戻ることができるようになりました.

目次に戻る

編集して再度コミットしてみよう

chapter1.txt ファイルに行を追加して,もう一度コミットしてみよう.具体的には chapter1.txt の最後に1行追加してみます.

chapter1.txtGitのお勉強
神戸学院大学
林坂ゼミ
Python

ファイルの状態を確認してからコミットします.

C:\Users\student\Documents\myReport>dir ⏎ # ファイルを確認する
 Volume in drive C has no label.
 Volume Serial Number is 9807-E930

 Directory of C:\Users\student\Documents\myReport

2024/03/24  14:16    <DIR>          .
2024/03/24  14:16    <DIR>          ..
2024/03/24  14:20                59 chapter1.txt
               1 File(s)             59 bytes
               2 Dir(s)  58,319,282,176 bytes free

C:\Users\student\Documents\myReport>type chapter1.txt ⏎ # ファイルの内容を確認
Gitのお勉強
神戸学院大学
林坂ゼミ
Python

C:\Users\student\Documents\myReport>git status ⏎ # chapter1.txt が修正(modify)されたことが分かる
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   chapter1.txt

no changes added to commit (use "git add" and/or "git commit -a")

C:\Users\student\Documents\myReport>git diff ⏎ # どこが変わったかの差分を確認する
diff --git a/chapter1.txt b/chapter1.txt
index 9b3b558..47e1b2b 100644
--- a/chapter1.txt
+++ b/chapter1.txt
@@ -1,3 +1,4 @@
 Gitのお勉強
 神戸学院大学
 林坂ゼミ
+Python

C:\Users\student\Documents\myReport>git add chapter1.txt ⏎ # ファイルをステージングエリアにアップ

C:\Users\student\Documents\myReport>git commit -m"1行追加" ⏎ # コミット
[main 804abdc] 1行追加
 1 file changed, 1 insertion(+)

C:\Users\student\Documents\myReport>git log ⏎ # ログの確認(2つのコミットがあることが分かる)
commit 804abdc728a61948f48581b8588d4d96622481f6 (HEAD -> main)
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:22:24 2024 +0900

    1行追加

commit ce2533a511d5f110eb5cc04f4c7571a49d8d7846
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:20:07 2024 +0900

    initial commit

C:\Users\student\Documents\myReport>

目次に戻る

さらに編集しつつ,新しいファイルも追加しよう

chapter1.txt をさらに編集しつつ,新しいファイル chapter2.txt も追加してみよう.まず,chapter1.txtの2行目に文字を追加,3行目を削除,最後の行を追加します.

chapter1.txtGitのお勉強
神戸学院大学経営学部
Python
React

また,chapter2.txt を作成し,次のような文章を入力してみます.

Windows を使って Git を勉強しよう
その後 Python Django や React のバージョン管理をしたい

2つのファイルを一気にまとめてコミットします.なお,複数のファイルをステージングエリアにアップするとき,git add ファイル名 を何度も入力するのは大変です.git add . を実行すると変更があったファイルや新しいファイルをまとめてステージングエリアにアップすることができます.

C:\Users\student\Documents\myReport>dir ⏎ # ファイル一覧
 Volume in drive C has no label.
 Volume Serial Number is 9807-E930

 Directory of C:\Users\student\Documents\myReport

2024/03/24  14:36    <DIR>          .
2024/03/24  14:36    <DIR>          ..
2024/03/24  14:36                64 chapter1.txt
2024/03/24  14:36               117 chapter2.txt
               2 File(s)            181 bytes
               2 Dir(s)  58,329,690,112 bytes free

C:\Users\student\Documents\myReport>type chapter1.txt ⏎ # ファイルの内容を閲覧
Gitのお勉強
神戸学院大学経営学部
Python
React

C:\Users\student\Documents\myReport>type chapter2.txt ⏎ # ファイルの内容を閲覧
Windows を使って Git を勉強しよう
その後 Python Django や React のバージョン管理をしたい

C:\Users\student\Documents\myReport>git status ⏎ # chapter1.txtが編集され,新しいファイル(chapter2.txt)がある
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   chapter1.txt

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

no changes added to commit (use "git add" and/or "git commit -a")

C:\Users\student\Documents\myReport>git add . ⏎ # すべての(編集・新規)ファイルをステージングエリアにアップする

C:\Users\student\Documents\myReport>git commit -m"chapter1修正とchapter2追加" ⏎ # コミットする
[main c779f65] chapter1修正とchapter2追加
 2 files changed, 4 insertions(+), 2 deletions(-)
 create mode 100644 chapter2.txt

C:\Users\student\Documents\myReport>

目次に戻る

コミットログを確認する

コミットが蓄積されたので,ログを確認してみよう.git log ではコミットのID, 作業者名,日時とコミットメッセージの一覧を確認できます.なお,各コミットには c779f65762e0282ced05017f9bed4cf6ded58ffa のようなハッシュ値を用いた ID が付与されていることに注意しよう.

C:\Users\student\Documents\myReport>git log ⏎
commit c779f65762e0282ced05017f9bed4cf6ded58ffa (HEAD -> main)
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:38:33 2024 +0900

    chapter1修正とchapter2追加

commit 804abdc728a61948f48581b8588d4d96622481f6
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:22:24 2024 +0900

    1行追加

commit ce2533a511d5f110eb5cc04f4c7571a49d8d7846
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:20:07 2024 +0900

    initial commit

C:\Users\student\Documents\myReport>

コミットの ID とコミットメッセージだけを表示したい場合は git log --oneline を使えば良いでしょう.このとき,コミット ID は先頭から7桁だけが表示されています(コミット ID を指定する場合もすべての桁を指定する必要はなく,一意に識別できれば十分なので先頭から7桁程度を指定するだけで大丈夫です).

C:\Users\student\Documents\myReport>git log --oneline ⏎
c779f65 (HEAD -> main) chapter1修正とchapter2追加
804abdc 1行追加
ce2533a initial commit

C:\Users\student\Documents\myReport>

コミットの内容を詳細に表示したければ,git log -p のように -p オプションを指定します.このとき,すべての情報が一画面に収まりきらない場合は,上下のカーソルキーやスペースキーでスクロールできます.また終了するには Qキーを押します.

C:\Users\student\Documents\myReport>git log -p ⏎
commit c779f65762e0282ced05017f9bed4cf6ded58ffa (HEAD -> main)
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:38:33 2024 +0900

    chapter1修正とchapter2追加

diff --git a/chapter1.txt b/chapter1.txt
index 47e1b2b..82b3a40 100644
--- a/chapter1.txt
+++ b/chapter1.txt
@@ -1,4 +1,4 @@
 Gitのお勉強
-神戸学院大学
-林坂ゼミ
+神戸学院大学経営学部
 Python
+React
diff --git a/chapter2.txt b/chapter2.txt
new file mode 100644
index 0000000..a2e81d8
--- /dev/null
+++ b/chapter2.txt
@@ -0,0 +1,2 @@
+Windows を使って Git を勉強しよう
+その後 Python Django や React のバージョン管理をしたい

commit 804abdc728a61948f48581b8588d4d96622481f6
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:22:24 2024 +0900

    1行追加

diff --git a/chapter1.txt b/chapter1.txt
index 9b3b558..47e1b2b 100644
--- a/chapter1.txt
+++ b/chapter1.txt
@@ -1,3 +1,4 @@
 Gitのお勉強
 神戸学院大学
 林坂ゼミ
+Python

commit ce2533a511d5f110eb5cc04f4c7571a49d8d7846
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:20:07 2024 +0900

    initial commit

diff --git a/chapter1.txt b/chapter1.txt
new file mode 100644
index 0000000..9b3b558
--- /dev/null
+++ b/chapter1.txt
@@ -0,0 +1,3 @@
+Gitのお勉強
+神戸学院大学
+林坂ゼミ

C:\Users\student\Documents\myReport>

表示したいコミットの数を限定することも可能です.例えば最新の2件のコミットだけを表示したいのであれば -2 というオプションを付与します.

C:\Users\student\Documents\myReport>git log -2 ⏎
commit c779f65762e0282ced05017f9bed4cf6ded58ffa (HEAD -> main)
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:38:33 2024 +0900

    chapter1修正とchapter2追加

commit 804abdc728a61948f48581b8588d4d96622481f6
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:22:24 2024 +0900

    1行追加

C:\Users\student\Documents\myReport>

オプションを組み合わせれば,最新1件のコミットについてだけ詳細に表示することも可能です.

C:\Users\student\Documents\myReport>git log -1 -p ⏎
commit c779f65762e0282ced05017f9bed4cf6ded58ffa (HEAD -> main)
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:38:33 2024 +0900

    chapter1修正とchapter2追加

diff --git a/chapter1.txt b/chapter1.txt
index 47e1b2b..82b3a40 100644
--- a/chapter1.txt
+++ b/chapter1.txt
@@ -1,4 +1,4 @@
 Gitのお勉強
-神戸学院大学
-林坂ゼミ
+神戸学院大学経営学部
 Python
+React
diff --git a/chapter2.txt b/chapter2.txt
new file mode 100644
index 0000000..a2e81d8
--- /dev/null
+++ b/chapter2.txt
@@ -0,0 +1,2 @@
+Windows を使って Git を勉強しよう
+その後 Python Django や React のバージョン管理をしたい

C:\Users\student\Documents\myReport>

目次に戻る

編集して上書き保存してしまったが元に戻したい

コミットのあとにいろいろ編集して上書き保存をしたが,やっぱり直前のコミットの状態に戻りたい(プログラムを書き換えたがエラーで動かなくなってしまったのでとりあえず動く状態まで戻したい,など)という状況を考えます.Git でバージョンを管理しておくと,このような状況でも簡単に元の状態に戻すことができます.これまでの作業のとおり,3つのコミットまでができた状態からスタートし,chapter1.txtの中身を確認します.

C:\Users\student\Documents\myReport>git log --oneline ⏎ # 3つのコミットがある
c779f65 (HEAD -> main) chapter1修正とchapter2追加
804abdc 1行追加
ce2533a initial commit

C:\Users\student\Documents\myReport>git status ⏎ # 最新のコミットの状態から変化がないことを確認
On branch main
nothing to commit, working tree clean

C:\Users\student\Documents\myReport>type chapter1.txt ⏎ # ファイルの中身を確認
Gitのお勉強
神戸学院大学経営学部
Python
React

C:\Users\student\Documents\myReport>

ファイルを適当に編集して保存した後,ファイルの中身を確認します.

C:\Users\student\Documents\myReport>type chapter1.txt ⏎
Gitのお勉強
神戸学院
Python
JavaScript and TypeScript

C:\Users\student\Documents\myReport>

コミットする前に変更を破棄して直前のコミットの状態に戻すには次の操作を行います.次の (1) では状態を確認しています.その結果,chapter1.txt ファイルの内容が変更されていることがわかります.(2) では chapter1.txt の変更を破棄して直前のコミットの状態に戻しています.(3) と (4) ではコミット時点の状態に戻っていることを確認しています.なお,Git で様々なコマンドを覚えることは大変ですが,(1) の git status コマンドを実行した出力の中にコマンドのヒントが幾つか表示されているので,それを参考にするとよいでしょう.

C:\Users\student\Documents\myReport>git status ⏎ # (1) ファイルが変更されていることがわかる
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   chapter1.txt

no changes added to commit (use "git add" and/or "git commit -a")

C:\Users\student\Documents\myReport>git restore chapter1.txt ⏎ # (2) 変更を破棄する

C:\Users\student\Documents\myReport>git status ⏎ # (3) クリーンな状態
On branch main
nothing to commit, working tree clean

C:\Users\student\Documents\myReport>type chapter1.txt ⏎ # (4) 編集前(コミット時点)の内容に戻った
Gitのお勉強
神戸学院大学経営学部
Python
React

C:\Users\student\Documents\myReport>

目次に戻る

編集してコミットしたが元に戻したい

編集してコミットしたけれども,直前のコミットや以前のコミットの状態に戻したいということもあります.ここでは,2つのファイルをそれぞれ編集・コミットした後,そのコミットを取り消して元に戻す操作を確認します.

まず,現在の状態を確認します.コミットが3つあることを確認し,それぞれのファイルの内容も確認しておきます.

C:\Users\student\Documents\myReport>git status ⏎ 
On branch main
nothing to commit, working tree clean

C:\Users\student\Documents\myReport>git log --oneline ⏎ 
c779f65 (HEAD -> main) chapter1修正とchapter2追加
804abdc 1行追加
ce2533a initial commit

C:\Users\student\Documents\myReport>type chapter1.txt ⏎ 
Gitのお勉強
神戸学院大学経営学部
Python
React

C:\Users\student\Documents\myReport>type chapter2.txt ⏎ 
Windows を使って Git を勉強しよう
その後 Python Django や React のバージョン管理をしたい

C:\Users\student\Documents\myReport>

次に2つのファイルにそれぞれ適当な編集を行い保存した後,中身を確認します.

C:\Users\student\Documents\myReport>type chapter1.txt ⏎
Gitのお勉強
誤った修正をしてみる
Python
React

C:\Users\student\Documents\myReport>type chapter2.txt ⏎
Windows を使って Git を勉強しよう
バグを埋め込んでみる
その後 Python Django や React のバージョン管理をしたい

C:\Users\student\Documents\myReport>

上で編集したファイルには問題がありそうですが,そのまま順番にコミットしてみます.なお,誤ってステージングエリアにアップしてしまった場合は get restore --staged ファイル名 でステージングエリアから取り除くことができます.

C:\Users\student\Documents\myReport>git status ⏎ # 状態を確認すると2つのファイルが更新されている
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   chapter1.txt
        modified:   chapter2.txt

no changes added to commit (use "git add" and/or "git commit -a")

C:\Users\student\Documents\myReport>git add . ⏎ # すべての更新ファイルをステージングエリアにまとめてアップする

C:\Users\student\Documents\myReport>git status ⏎ # 誤ってchapter2.txtもステージングエリアにアップしてしまった
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   chapter1.txt
        modified:   chapter2.txt


C:\Users\student\Documents\myReport>git restore --staged chapter2.txt ⏎ # chapter2.txt をステージングエリアから取り除く

C:\Users\student\Documents\myReport>git status ⏎ # 状態を確認すると chapter1.txt だけがステージングエリアにある
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   chapter1.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   chapter2.txt


C:\Users\student\Documents\myReport>git commit -m"chapter1を編集" ⏎ # chapter1.txt だけをコミット
[main d56c2d2] chapter1を編集
 1 file changed, 1 insertion(+), 1 deletion(-)

C:\Users\student\Documents\myReport>git add chapter2.txt ⏎ # chapter2.txt をステージングエリアにアップ

C:\Users\student\Documents\myReport>git commit -m"chapter2を編集" ⏎ # chapter2.txt をコミット
[main 0db733f] chapter2を編集
 1 file changed, 1 insertion(+)

C:\Users\student\Documents\myReport>git log --oneline ⏎ # 2つのコミットが追加されたことがわかる
0db733f (HEAD -> main) chapter2を編集
d56c2d2 chapter1を編集
c779f65 chapter1修正とchapter2追加
804abdc 1行追加
ce2533a initial commit

C:\Users\student\Documents\myReport>

2つのコミットを行ったので,その詳細な情報を確認しよう.その結果,間違った修正を行ってコミットしてしまったことがわかりました.

C:\Users\student\Documents\myReport>git log -2 -p ⏎
commit 0db733fb81d710b3cd91ab4fd3ea069f84b4642e (HEAD -> main)
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:46:03 2024 +0900

    chapter2を編集

diff --git a/chapter2.txt b/chapter2.txt
index a2e81d8..cb492df 100644
--- a/chapter2.txt
+++ b/chapter2.txt
@@ -1,2 +1,3 @@
 Windows を使って Git を勉強しよう
+バグを埋め込んでみる
 その後 Python Django や React のバージョン管理をしたい

commit d56c2d2ef8377a1e67232cc9f678b435684ab66d
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 14:45:42 2024 +0900

    chapter1を編集

diff --git a/chapter1.txt b/chapter1.txt
index 82b3a40..5eced0a 100644
--- a/chapter1.txt
+++ b/chapter1.txt
@@ -1,4 +1,4 @@
 Gitのお勉強
-神戸学院大学経営学部
+誤った修正をしてみる
 Python
 React

C:\Users\student\Documents\myReport>

上の2つのコミットには問題があったので,コミット ID = c779f65 の状態に戻したいとしましょう.なお,コミットには c779f65762e0282ced05017f9bed4cf6ded58ffa のような ID が付けられていますが,git reset コマンドなどでコミット ID を指定する際には,ID の先頭から7桁程度をコピーして指定すれば動作します.

C:\Users\student\Documents\myReport>git log --oneline ⏎ # ログの一覧を表示し,どのコミットまで戻るかを考える
0db733f (HEAD -> main) chapter2を編集
d56c2d2 chapter1を編集
c779f65 chapter1修正とchapter2追加
804abdc 1行追加
ce2533a initial commit

C:\Users\student\Documents\myReport>git reset --hard c779f65 ⏎ # 特定のコミットまで戻る
HEAD is now at c779f65 chapter1修正とchapter2追加

C:\Users\student\Documents\myReport>git status ⏎ # 作業ディレクトリはクリーンな状態
On branch main
nothing to commit, working tree clean

C:\Users\student\Documents\myReport>git log --oneline ⏎ # コミットログは希望のコミットまで戻っている
c779f65 (HEAD -> main) chapter1修正とchapter2追加
804abdc 1行追加
ce2533a initial commit

C:\Users\student\Documents\myReport>type chapter1.txt ⏎ # ファイルの内容も元に戻っている
Gitのお勉強
神戸学院大学経営学部
Python
React

C:\Users\student\Documents\myReport>type chapter2.txt ⏎ # ファイルの内容も元に戻っている
Windows を使って Git を勉強しよう
その後 Python Django や React のバージョン管理をしたい

C:\Users\student\Documents\myReport>

Python Django, Laravel などを用いたシステム開発では,Gitのバージョン管理を用いて概ね次の手順を繰り返していくことになるでしょう.

  1. ソースファイルを編集する
  2. テストを実施してバグがないことを確認する
  3. Gitでコミットする

このような操作を繰り返すことで,もしも変な編集をしてシステムが起動しなくなっても,動作するバージョンにいつでも戻ることができます.また,いつでも戻れるという安心感があるので,思い切ったコードの変更やリファクタリングを行うこともできるようになります.

目次に戻る