Git コミット済みリポジトリで、メッセージコミットだけのブランチを作成

無料ソースコード管理 (SCM: software configuration management) ソフト Git で、既存のコミットを持たない、メッセージだけのコミットだけを持つブランチの作成手順を紹介します。
メッセージだけのコミットを行うために、作成したブランチの作業ディレクトリのコミット対象のファイルと index を消去する手順も紹介します。

※失敗例も紹介しています。成功パターンだけを読みたい場合は、「作業ツリーのファイルを削除してから、メッセージだけをコミット」の章を参照してください。

git-scm 公式サイトの一部 20250408

Git-2.49.0 (64 bit) を使用します。

前回の記事

前回は、 Git で作成したブランチを削除する手順を紹介しました。
削除するブランチのコミットが、他のブランチにマージ済みかの確認の手順も紹介しました。

コミット履歴をもたないブランチの作成

すでにコミット履歴がある状態で、後述する git branch コマンドでブランチを作成すると、最新のコミットの上にブランチが作成されてしまいます。

すでにコミット履歴があるリポジトリで、コミット履歴を持たないブランチを作成するには、git checkout コマンドの –orphan オプションを使います。

git checkout --orphan master

git checkout –orphan を用いると、指定した名前のブランチ作成され、直後に現在のブランチが、そのブランチに切り替えられます。

–orphan <new-branch>

Create a new unborn branch, named <new-branch>, started from <start-point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

<start-point> から開始する、 という名前の新しい未誕生ブランチを作成し、それに切り替えます。この新しいブランチ上で作成された最初のコミットには親がなく、他のすべてのブランチやコミットから完全に切り離された新しい履歴のルートになります。

Git – git-checkout Documentation と Google 翻訳

例では、すべてのコミットから切り離された master ブランチ作成します。
実行すると、作成された master ブランチに切り替わりました

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git checkout --orphan master
Switched to a new branch 'master'

ブランチは、最低1つのコミットを起点にします。

そのため、作成して切り替えられた master ブランチに、以下の git commit コマンドで –allow-empty と -m オプションを指定して、メッセージだけのコミットを行います。

git commit --allow-empty -m "コミットについてのメッセージ"

--allow-empty

Usually recording a commit that has the exact same tree as its sole parent commit is a mistake, and the command prevents you from making such a commit. This option bypasses the safety, and is primarily for use by foreign SCM interface scripts.

通常、唯一の親コミットとまったく同じツリーを持つコミットを記録するのは間違いであり、このコマンドはそのようなコミットを行うことを妨げます。このオプションは安全性をバイパスし、主に外部 SCM インターフェイス スクリプトによって使用されます。

-m <msg>
--message=<msg>

Use <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

<msg> をコミットメッセージとして使用します。複数の -m オプションが指定された場合、それらの値は別の段落として連結されます。

The -m option is mutually exclusive with -c-C, and -F.

Git – git-commit Documentation

以下は、コマンドの実行結果です。
メッセージだけのコミットをする予定でしたが、 create mode から始まるいくつものファイルパスが結果に表示されています。

これは、既存のリポジトリにはコミット予定のファイル群があった(ステージングエリアに登録してあったファイル群が index に記録されていた)ため、それらのファイルの追加もコミットに入ってしまったことを表しています。

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git commit --allow-empty -m "Initial empty master"
[master (root-commit) 65e7135] Initial empty master
 22 files changed, 2873 insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 .gitignore
 create mode 100644 LICENSE
 create mode 100644 README.md
 create mode 100644 study/fit_font_size/study_fit_font_size.gd
 create mode 100644 study/fit_font_size/study_fit_font_size.tscn
 create mode 100644 study/fit_font_size/study_fit_font_size_backup.tscn
 create mode 100644 study/fit_font_size/study_popup_sc_fit_font_size_dialog.gd
 create mode 100644 study/fit_font_size/study_popup_sc_fit_font_size_dialog.tscn
 create mode 100644 study/study_input_key.gd
 create mode 100644 study/study_input_key.tscn
 create mode 100644 study/study_input_key.tscn~RF6d1dd68.TMP
 create mode 100644 study/study_pseudo_varargs.gd
 create mode 100644 study/study_pseudo_varargs.tscn
 create mode 100644 study/study_setter_getter_signal.gd
 create mode 100644 study/study_setter_getter_signal.tscn
 create mode 100644 utility/sc_min_max_constraint_handler.gd
 create mode 100644 utility/sc_min_max_constraint_handler_inspector.gd
 create mode 100644 utility/sc_rich_text_url_handler.gd
 create mode 100644 utility/sc_util.gd
 create mode 100644 utility/test/test_sc_util.gd
 create mode 100644 utility_editor/sc_util_editor.gd

create mode から始まるファイルは、意図せず develop ブランチ(切り替える前のブランチ)でステージングエリアに追加しておいたものでした。

TortoiseGit で現状の確認

リポジトリフォルダをエクスプローラで開いて、右クリックで表示されるメニュー「TortoiseGit」→「リビジョングラフ」を選択します。

コミット済みの既存リポジトリで Git Flow のブランチモデルに変更1

「リビジョングラフ」ダイアログを表示すると、 develop ブランチと master ブランチが同じコミット履歴を持っています。
※赤い枠の develop が現在のブランチです。

コミット済みの既存リポジトリで Git Flow のブランチモデルに変更2

TortoiseGit の「リビジョンログ(ログメッセージ)」ダイアログでも、メッセージの他にファイルが追加されていることが確認できます。
「リビジョンログ(ログメッセージ)」ダイアログを表示するには、リポジトリフォルダをエクスプローラで開いて、右クリックで表示されるメニュー「TortoiseGit」→「ログを表示」を選択します。

コミット済みの既存リポジトリで Git Flow のブランチモデルに変更3

コミットをやり直すため、作成したブランチを削除

メッセージだけのコミットを起点にした master ブランチを作成するために、もう一度 git checkout –orphan コマンドを実行します。

git checkout --orphan master

すでに作成されているブランチ名のためエラーが起きて、ブランチの削除に失敗しました。

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git checkout --orphan master
fatal: a branch named 'master' already exists

前回紹介した、削除するブランチのコミットが、他のブランチへマージ済みではない場合でも強制的に削除できる -D オプションを用いて

git branch -D master

で消そうとしても、現在のブランチのため削除できません

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git branch -D master
error: cannot delete branch 'master' used by worktree at 'G:/Dev/Godot4GD/SakuraCrowd/SakuraCrowdGodotLib'

以下の git switch コマンドでブランチを、削除するブランチ以外に切り替えます。

git switch 切り替え先のブランチ名

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

指定されたブランチに切り替えます。作業ツリーとインデックスはブランチに一致するように更新されます。すべての新しいコミットはこのブランチの先端に追加されます。

Git – git-switch Documentation と Google 翻訳

もう一つのブランチ develop に切り替えました

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git switch develop
Switched to branch 'develop'

改めて、 master ブランチを削除します。

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git branch -D master
Deleted branch master (was 65e7135).

作業ツリーのファイルを削除してから、メッセージだけをコミット

再び、git checkout コマンドの –orphan オプションを使って、どのコミットも持たない master ブランチを作成して、そのブランチに切り替えます。

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git checkout --orphan master
Switched to a new branch 'master'

ここで、git rm コマンドで、作業ディレクトリの git に管理されているファイルを削除して、インデックスを消去します。
-r オプションで再帰的に下位のフォルダの内容も削除します。
-f オプションは強制的にすることで、確認のやりとりを省略します。

git rm -rf .

Remove files from the working tree and from the index
作業ツリーとインデックスからファイルを削除する

-r
Allow recursive removal when a leading directory name is given.
先頭のディレクトリ名が指定されている場合、再帰的な削除を許可します。

-f–force
Override the up-to-date check.
最新のチェックをオーバーライドします。

Git – git-rm Documentation と Google 翻訳

Git 管理されているファイル作業ディレクトリから削除されました。
ステージングエリアでコミット対象になっていたファイル群の情報が書かれている index も消去されました。

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git rm -rf .
rm '.gitattributes'
rm '.gitignore'
rm 'LICENSE'
rm 'README.md'
rm 'study/fit_font_size/study_fit_font_size.gd'
rm 'study/fit_font_size/study_fit_font_size.tscn'
rm 'study/fit_font_size/study_fit_font_size_backup.tscn'
rm 'study/fit_font_size/study_popup_sc_fit_font_size_dialog.gd'
rm 'study/fit_font_size/study_popup_sc_fit_font_size_dialog.tscn'
rm 'study/study_input_key.gd'
rm 'study/study_input_key.tscn'
rm 'study/study_input_key.tscn~RF6d1dd68.TMP'
rm 'study/study_pseudo_varargs.gd'
rm 'study/study_pseudo_varargs.tscn'
rm 'study/study_setter_getter_signal.gd'
rm 'study/study_setter_getter_signal.tscn'
rm 'utility/sc_min_max_constraint_handler.gd'
rm 'utility/sc_min_max_constraint_handler_inspector.gd'
rm 'utility/sc_rich_text_url_handler.gd'
rm 'utility/sc_util.gd'
rm 'utility/test/test_sc_util.gd'
rm 'utility_editor/sc_util_editor.gd'

作業ディレクトリと index に何もない状態に作業ツリーを変更してから、git checkout –orphan で作成して切り替えた master ブランチメッセージのみのコミットを与えます。

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git commit --allow-empty -m "Initial empty master"
[master (root-commit) b240982] Initial empty master

TortoiseGit での確認

git rm -rf . を行った結果、 Git 管理されているファイルは、現在の master ブランチの作業ディレクトリでは削除されました。
削除された Git 管理のファイル群は、別のブランチに切り替えれば、そのブランチのコミット内容に応じて復元されます。

リポジトリフォルダをエクスプローラで開いて、右クリックで表示されるメニュー「TortoiseGit」→「リビジョングラフ」を選択して、「リビジョングラフ」ダイアログを開いてみましょう。

コミット済みの既存リポジトリで Git Flow のブランチモデルに変更4

メッセージだけのコミットを持つ master ブランチは、リビジョングラフには表示されていません

コミット済みの既存リポジトリで Git Flow のブランチモデルに変更5

次にメニュー「TortoiseGit」→「ログを表示」で「リビジョンログ(ログメッセージ)」ダイアログを開いてみましょう。

コミット済みの既存リポジトリで Git Flow のブランチモデルに変更6

「リビジョンログ(ログメッセージ)」ダイアログには、ファイルの変更のない、メッセージだけのコミットをもつ master ブランチが確認できました。

コミット済みの既存リポジトリで Git Flow のブランチモデルに変更7

まとめ

今回は、無料ソースコード管理 (SCM: software configuration management) ソフト Git で、既存のコミットを持たない、メッセージだけのコミットだけを持つブランチの作成手順を紹介しました。
メッセージだけのコミットを行うために、作成したブランチの作業ディレクトリのコミット対象のファイルと index を消去する手順も紹介しました。

参照サイト Thank You!

記事一覧 → Compota-Soft-Press

コメント

Ads Blocker Image Powered by Code Help Pro

お願い - Ads Blocker Detected

このサイトは広告を掲載して運営しています。

ポップアップを閉じて閲覧できますが、よろしければ

このサイト内の広告を非表示にする拡張機能をオフにしていただけませんか?

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

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