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

Git 入門トップページ

« 戻る 次へ »

Git によるバージョン管理入門 (Mac / Linux)

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

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

目次に戻る

ここでのストーリー

ここでは,「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」という名称の隠しフォルダが作成されます.

student@MacBookAir2023 ~ % pwd ⏎ # 現在のワーキングディレクトリ(フォルダ)を表示 (Print Working Directory)
/Users/student
student@MacBookAir2023 ~ % cd Documents ⏎ # Documents フォルダに移動
student@MacBookAir2023 Documents % mkdir myReport ⏎ # myReport フォルダを作成
student@MacBookAir2023 Documents % cd myReport ⏎ # myReport フォルダに移動
student@MacBookAir2023 myReport % pwd ⏎ # 現在のワーキングディレクトリを表示
/Users/student/Documents/myReport
student@MacBookAir2023 myReport % git init ⏎ # git のプロジェクトを作成(初期化)する
Initialized empty Git repository in /Users/student/Documents/myReport/.git/
student@MacBookAir2023 myReport % ls ⏎ # ファイル一覧の表示
student@MacBookAir2023 myReport % ls -l ⏎ # ファイル一覧の詳細表示
total 0
student@MacBookAir2023 myReport % ls -al ⏎ # 隠しファイルも含めて一覧の詳細表示
total 0
drwxr-xr-x@ 3 student  staff   96  3 24 17:05 .
drwx------+ 4 student  staff  128  3 24 17:05 ..
drwxr-xr-x@ 9 student  staff  288  3 24 17:05 .git
student@MacBookAir2023 myReport %

なお,Visual Studio Code を利用する場合は,コマンドプロンプトでプロジェクトのフォルダまで移動した状態で code . コマンドを実行すると Visual Studio Code が起動し,そのフォルダを開くことができます.このとき code . コマンドが認識されなければここを参照して PATH 内にコマンドをインストールしてください.

student@MacBookAir2023 myReport % code . ⏎
student@MacBookAir2023 myReport %

目次に戻る

chapter1.txtを作成する

まずは,myReport フォルダに「chapter1.txt」ファイルを作成します.任意のテキストエディタ (Visual Studio Code など) を使って次のようなテキストファイルを作成するとよいでしょう.

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

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

目次に戻る

コミットしてみよう

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

student@MacBookAir2023 myReport % ls -l ⏎ # ファイルの確認
total 8
-rw-r--r--  1 student  staff  48  3 24 17:07 chapter1.txt
student@MacBookAir2023 myReport % cat chapter1.txt ⏎ # ファイルの内容を表示する
Gitのお勉強
神戸学院大学
林坂ゼミ
student@MacBookAir2023 myReport % cat -n chapter1.txt ⏎ # 行番号を付けてファイルの内容を表示する
     1	Gitのお勉強
     2	神戸学院大学
     3	林坂ゼミ
student@MacBookAir2023 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)
student@MacBookAir2023 myReport % git add chapter1.txt ⏎ # ファイルをステージングエリアにアップする
student@MacBookAir2023 myReport % git status ⏎ # コミットの準備ができていることを確認する
On branch main

No commits yet

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

student@MacBookAir2023 myReport % git commit -m"initial commit" ⏎ # コミットしてみる
[main (root-commit) c381e43] initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 chapter1.txt
student@MacBookAir2023 myReport % git log ⏎ # コミットログを確認する
commit c381e439c474324c977df220eda2a58146953f38 (HEAD -> main)
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:09:22 2024 +0900

    initial commit
student@MacBookAir2023 myReport %

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

目次に戻る

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

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

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

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

student@MacBookAir2023 myReport % ls -l ⏎ # ファイルを確認する
total 8
-rw-r--r--  1 student  staff  55  3 24 17:09 chapter1.txt
student@MacBookAir2023 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")
student@MacBookAir2023 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
student@MacBookAir2023 myReport % git add chapter1.txt ⏎ # ファイルをステージングエリアにアップ
student@MacBookAir2023 myReport % git commit -m"1行追加" ⏎ # コミット
[main b94159a] 1行追加
 1 file changed, 1 insertion(+)
student@MacBookAir2023 myReport % git log ⏎ # ログの確認(2つのコミットがあることが分かる)
commit b94159a3aa3d9ce386419e773e7c47bd913bbecc (HEAD -> main)
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:10:48 2024 +0900

    1行追加

commit c381e439c474324c977df220eda2a58146953f38
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:09:22 2024 +0900

    initial commit
student@MacBookAir2023 myReport %

目次に戻る

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

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

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

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

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

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

student@MacBookAir2023 myReport % ls -l ⏎ # ファイル一覧
total 16
-rw-r--r--@ 1 student  staff   60  3 24 17:15 chapter1.txt
-rw-r--r--  1 student  staff  111  3 24 17:16 chapter2.txt
student@MacBookAir2023 myReport % cat chapter1.txt ⏎ # ファイルの内容を閲覧
Gitのお勉強
神戸学院大学経営学部
Python
React
student@MacBookAir2023 myReport % cat chapter2.txt ⏎ # ファイルの内容を閲覧
Mac を使って Git を勉強しよう
その後 Python Django や React のバージョン管理をしたい
student@MacBookAir2023 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")
student@MacBookAir2023 myReport % git add . ⏎ # すべての(編集・新規)ファイルをステージングエリアにアップする
student@MacBookAir2023 myReport % git commit -m"chapter1修正とchapter2追加" ⏎ # コミットする
[main 15e6014] chapter1修正とchapter2追加
 2 files changed, 4 insertions(+), 2 deletions(-)
 create mode 100644 chapter2.txt
student@MacBookAir2023 myReport %

目次に戻る

コミットログを確認する

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

student@MacBookAir2023 myReport % git log ⏎
commit 15e60143d972e36cc2b6917e4c005b79bd916b08 (HEAD -> main)
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:17:21 2024 +0900

    chapter1修正とchapter2追加

commit b94159a3aa3d9ce386419e773e7c47bd913bbecc
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:10:48 2024 +0900

    1行追加

commit c381e439c474324c977df220eda2a58146953f38
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:09:22 2024 +0900

    initial commit
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 myReport % git log --oneline ⏎
15e6014 (HEAD -> main) chapter1修正とchapter2追加
b94159a 1行追加
c381e43 initial commit
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 myReport % git log -p ⏎
commit 15e60143d972e36cc2b6917e4c005b79bd916b08 (HEAD -> main)
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:17:21 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..739bb54
--- /dev/null
+++ b/chapter2.txt
@@ -0,0 +1,2 @@
+Mac を使って Git を勉強しよう
+その後 Python Django や React のバージョン管理をしたい

commit b94159a3aa3d9ce386419e773e7c47bd913bbecc
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:10:48 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 c381e439c474324c977df220eda2a58146953f38
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:09:22 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のお勉強
+神戸学院大学
+林坂ゼミ
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 myReport % git log -2 ⏎
commit 15e60143d972e36cc2b6917e4c005b79bd916b08 (HEAD -> main)
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:17:21 2024 +0900

    chapter1修正とchapter2追加

commit b94159a3aa3d9ce386419e773e7c47bd913bbecc
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:10:48 2024 +0900

    1行追加
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 myReport % git log -1 -p ⏎
commit 15e60143d972e36cc2b6917e4c005b79bd916b08 (HEAD -> main)
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:17:21 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..739bb54
--- /dev/null
+++ b/chapter2.txt
@@ -0,0 +1,2 @@
+Mac を使って Git を勉強しよう
+その後 Python Django や React のバージョン管理をしたい
student@MacBookAir2023 myReport %

目次に戻る

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

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

student@MacBookAir2023 myReport % git log --oneline ⏎ # 3つのコミットがある
15e6014 (HEAD -> main) chapter1修正とchapter2追加
b94159a 1行追加
c381e43 initial commit
student@MacBookAir2023 myReport % git status ⏎ # 最新のコミットの状態から変化がないことを確認
On branch main
nothing to commit, working tree clean
student@MacBookAir2023 myReport % cat chapter1.txt ⏎ # ファイルの中身を確認
Gitのお勉強
神戸学院大学経営学部
Python
React
student@MacBookAir2023 myReport %
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>

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

student@MacBookAir2023 myReport % cat chapter1.txt ⏎
Gitのお勉強
神戸学院
Python
React
JavaScript and TypeScript
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 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")
student@MacBookAir2023 myReport % git restore chapter1.txt ⏎ # (2) 変更を破棄する
student@MacBookAir2023 myReport % git status ⏎ # (3) クリーンな状態
On branch main
nothing to commit, working tree clean
student@MacBookAir2023 myReport % cat chapter1.txt ⏎ # (4) 編集前(コミット時点)の内容に戻った
Gitのお勉強
神戸学院大学経営学部
Python
React
student@MacBookAir2023 myReport %

目次に戻る

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

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

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

student@MacBookAir2023 myReport % git status ⏎ 
On branch main
nothing to commit, working tree clean
student@MacBookAir2023 myReport % git log --oneline ⏎ 
15e6014 (HEAD -> main) chapter1修正とchapter2追加
b94159a 1行追加
c381e43 initial commit
student@MacBookAir2023 myReport % cat chapter1.txt ⏎ 
Gitのお勉強
神戸学院大学経営学部
Python
React
student@MacBookAir2023 myReport % cat chapter2.txt ⏎ 
Mac を使って Git を勉強しよう
その後 Python Django や React のバージョン管理をしたい
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 myReport % cat chapter1.txt ⏎
Gitのお勉強
誤った修正をしてみる
Python
React
student@MacBookAir2023 myReport % cat chapter2.txt ⏎
Mac を使って Git を勉強しよう
バグを埋め込んでみる
その後 Python Django や React のバージョン管理をしたい
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 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")
student@MacBookAir2023 myReport % git add . ⏎ # すべての更新ファイルをステージングエリアにまとめてアップする
student@MacBookAir2023 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

student@MacBookAir2023 myReport % git restore --staged chapter2.txt ⏎ # chapter2.txt をステージングエリアから取り除く
student@MacBookAir2023 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

student@MacBookAir2023 myReport % git commit -m"chapter1を編集" ⏎ # chapter1.txt だけをコミット
[main d9bf314] chapter1を編集
 1 file changed, 1 insertion(+), 1 deletion(-)
student@MacBookAir2023 myReport % git add chapter2.txt ⏎ # chapter2.txt をステージングエリアにアップ
student@MacBookAir2023 myReport % git commit -m"chapter2を編集" ⏎ # chapter2.txt をコミット
[main f0f5cbf] chapter2を編集
 1 file changed, 1 insertion(+)
student@MacBookAir2023 myReport % git log --oneline ⏎ # 2つのコミットが追加されたことがわかる
f0f5cbf (HEAD -> main) chapter2を編集
d9bf314 chapter1を編集
15e6014 chapter1修正とchapter2追加
b94159a 1行追加
c381e43 initial commit
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 myReport % git log -2 -p ⏎
commit f0f5cbfae1adab3209eee0b84e767b0653d623a3 (HEAD -> main)
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:24:59 2024 +0900

    chapter2を編集

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

commit d9bf3145ff0c769c5ebe47ab284c64b50cf54eec
Author: Gakuin Hana <gakuin.hana@dummy.kobegakuin.ac.jp>
Date:   Sun Mar 24 17:24:45 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
student@MacBookAir2023 myReport %

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

student@MacBookAir2023 myReport % git log --oneline ⏎ # ログの一覧を表示し,どのコミットまで戻るかを考える
f0f5cbf (HEAD -> main) chapter2を編集
d9bf314 chapter1を編集
15e6014 chapter1修正とchapter2追加
b94159a 1行追加
c381e43 initial commit
student@MacBookAir2023 myReport % git reset --hard 15e6014 ⏎ # 特定のコミットまで戻る
HEAD is now at 15e6014 chapter1修正とchapter2追加
student@MacBookAir2023 myReport % git status ⏎ # 作業ディレクトリはクリーンな状態
On branch main
nothing to commit, working tree clean
student@MacBookAir2023 myReport % git log --oneline ⏎ # コミットログは希望のコミットまで戻っている
15e6014 (HEAD -> main) chapter1修正とchapter2追加
b94159a 1行追加
c381e43 initial commit
student@MacBookAir2023 myReport % cat chapter1.txt ⏎ # ファイルの内容も元に戻っている
Gitのお勉強
神戸学院大学経営学部
Python
React
student@MacBookAir2023 myReport % cat chapter2.txt ⏎ # ファイルの内容も元に戻っている
Mac を使って Git を勉強しよう
その後 Python Django や React のバージョン管理をしたい
student@MacBookAir2023 myReport %

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

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

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

目次に戻る