Git ブランチ管理

Git ブランチ管理は Git の強力な機能の1つであり、複数の開発者が並行して作業し、新機能の開発、バグ修正、実験をメインコードベースに影響を与えずに行うことができます。

ほぼすべてのバージョン管理システムは何らかの形でブランチをサポートしており、1つのブランチは独立した開発ラインを表します。

ブランチを使用するということは、開発のメインラインから分離し、メインラインに影響を与えずに作業を続けることができることを意味します。

Git のブランチは、実際には変更スナップショットを指すポインタです。

Git のブランチモデルは「キラーフィーチャー」と呼ばれており、まさにそのおかげで、Git をGitバージョン管理システムの仲間から際立たせています。

ブランチの作成

新しいブランチを作成し、そのブランチに切り替える:

git checkout -b <branchname>

例:

git checkout -b feature-xyz

ブランチ切り替えコマンド:

git checkout (branchname)

例:

git checkout main

ブランチを切り替えるとき、Git はそのブランチの最後のコミットのスナップショットで作業ディレクトリの内容を置き換えます。そのため、複数のブランチに複数のディレクトリは必要ありません。

ブランチの確認

すべてのブランチを確認:

git branch

リモートブランチを確認:

git branch -r

すべてのローカルおよびリモートブランチを確認:

git branch -a

ブランチのマージ

他のブランチを現在のブランチにマージする:

git merge <branchname>

例:main ブランチに切り替えて feature-xyz ブランチをマージする:

git checkout main
git merge feature-xyz

マージコンフリクトの解決

マージ中にコンフリクトが発生すると、Git はコンフリクトのあるファイルをマークし、手動で解決する必要があります。

コンフリクトファイルを開き、マークに従ってコンフリクトを解決します。

コンフリクト解決を完了としてマーク:

git add <conflict-file>

マージ結果をコミット:

git commit

ブランチの削除

ローカルブランチの削除:

git branch -d <branchname>

マージされていないブランチを強制削除:

git branch -D <branchname>

リモートブランチの削除:

git push origin --delete <branchname>

例

始める前に、テスト用ディレクトリを作成しましょう:

$ mkdir gitdemo
$ cd gitdemo/
$ git init
Initialized empty Git repository...
$ touch README
$ git add README
$ git commit -m '第一次版本提交'
[master (root-commit) 3b58100] 第一次版本提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

Git ブランチ管理

ブランチの一覧表示

ブランチ一覧の基本コマンド:

git branch

引数なしの場合、git branchローカルのブランチが一覧表示されます。

$ git branch
* master

この例の意味は、私たちが「master」というブランチを持っており、そのブランチが現在のブランチであることを意味します。

あなたがgit initを実行すると、デフォルトで Git が「master」ブランチを作成します。

手動でブランチを作成する場合は、git branch (branchname)を実行します。

$ git branch testing
$ git branch
* master
  testing

新しいブランチができたのがわかります。testing。

この方法で前回のコミット後に新しいブランチを作成し、その後さらにコミットが追加され、そして「testing」ブランチに切り替えると、Git は作業ディレクトリをブランチ作成時の状態に戻します。

次に、ブランチの切り替え方をデモします。git checkout (branch) で変更したいブランチに切り替えます。

$ ls
README
$ echo 'example.com' > test.txt
$ git add .
$ git commit -m 'add test.txt'
[master 3e92c19] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ ls
README        test.txt
$ git checkout testing
Switched to branch 'testing'
$ ls
README

「testing」ブランチに切り替えると、追加した新しいファイル test.txt が削除されました。「master」ブランチに戻すと、それらは再び現れました。

$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt

git checkout -b (branchname) コマンドを使用して、新しいブランチを作成し、すぐにそのブランチに切り替えて操作することもできます。

$ git checkout -b newtest
Switched to a new branch 'newtest'
$ git rm test.txt 
rm 'test.txt'
$ ls
README
$ touch サンプル.php
$ git add .
$ git commit -am 'removed test.txt、add サンプル.php'
[newtest c1501a2] removed test.txt、add サンプル.php
 2 files changed, 1 deletion(-)
 create mode 100644 サンプル.php
 delete mode 100644 test.txt
$ ls
README        サンプル.php
$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt

ご覧のとおり、ブランチを作成し、そのブランチで一部のファイル test.txt を削除し、サンプル.php ファイルを追加しました。その後メインブランチに切り替えると、削除した test.txt ファイルは元に戻り、新しく追加した サンプル.php はメインブランチには存在しません。

ブランチを使用して作業を分離することで、異なる開発環境で作業を行い、行ったり来たり切り替えることができます。

ブランチの削除

ブランチ削除コマンド:

git branch -d (branchname)

たとえば、testing ブランチを削除する場合:

$ git branch
* master
  testing
$ git branch -d testing
Deleted branch testing (was 85fc7e7).
$ git branch
* master

ブランチのマージ

ブランチに独立した内容が含まれるようになったら、いずれそれをメインブランチにマージしたくなるでしょう。次のコマンドを使用して、任意のブランチを現在のブランチにマージできます:

git merge
$ git branch
* master
  newtest
$ ls
README        test.txt
$ git merge newtest
Updating 3e92c19..c1501a2
Fast-forward
 サンプル.php | 0
 test.txt   | 1 -
 2 files changed, 1 deletion(-)
 create mode 100644 サンプル.php
 delete mode 100644 test.txt
$ ls
README        サンプル.php

上記の例では、newtest ブランチをメインブランチにマージし、test.txt ファイルを削除しました。

マージが完了したら、ブランチを削除できます:

$ git branch -d newtest
Deleted branch newtest (was c1501a2).

削除後、master ブランチだけが残ります:

$ git branch
* master

マージコンフリクト

マージは単純なファイルの追加・削除だけでなく、Git は変更もマージします。

$ git branch
* master
$ cat サンプル.php

注意:サンプル.php がない場合は、まずこのファイルを作成する必要があります。コマンドは次のとおりです。touch サンプル.php。

まず、change_site というブランチを作成して切り替え、サンプル.php の内容を次のように変更します:

<?php
echo 'サンプル';
?>

change_site ブランチを作成:

$ git checkout -b change_site
Switched to a new branch 'change_site'
$ vim サンプル.php
$ head -3 サンプル.php
<?php
echo 'サンプル';
?>
$ git commit -am 'changed the サンプル.php'
[change_site 7774248] changed the サンプル.php
 1 file changed, 3 insertions(+)
 

vim コマンドの操作については、次を参照してください:Linux vi/vim。

変更内容を change_site ブランチにコミットします。ここで master ブランチに切り替えると、内容が変更前(空ファイル、コードなし)に戻っていることがわかります。再度 サンプル.php ファイルを変更します。

$ git checkout master
Switched to branch 'master'
$ cat サンプル.php
$ vim サンプル.php    # 修改内容如下
$ cat サンプル.php
<?php
echo 1;
?>
$ git diff
diff --git a/サンプル.php b/サンプル.php
index e69de29..ac60739 100644
--- a/サンプル.php
+++ b/サンプル.php
@@ -0,0 +1,3 @@
+<?php
+echo 1;
+?>
$ git commit -am '修改代码'
[master c68142b] 修改代码
 1 file changed, 3 insertions(+)

これでこれらの変更は「master」ブランチに記録されました。次に、「change_site」ブランチをマージします。

$ git merge change_site
Auto-merging サンプル.php
CONFLICT (content): Merge conflict in サンプル.php
Automatic merge failed; fix conflicts and then commit the result.

$ cat サンプル.php     # 打开文件,看到冲突内容
<?php
<<<<<<< HEAD
echo 1;
=======
echo 'サンプル';
>>>>>>> change_site
?>

前のブランチを master ブランチにマージすると、マージコンフリクトが発生しました。次に手動で修正する必要があります。

$ vim サンプル.php 
$ cat サンプル.php
<?php
echo 1;
echo 'サンプル';
?>
$ git diff
diff --cc サンプル.php
index ac60739,b63d7d7..0000000
--- a/サンプル.php
+++ b/サンプル.php
@@@ -1,3 -1,3 +1,4 @@@
  <?php
 +echo 1;
+ echo 'サンプル';
  ?>

Git では、git add を使用して、ファイルのコンフリクトが解決済みであることを Git に伝えることができます。

$ git status -s
UU サンプル.php
$ git add サンプル.php
$ git status -s
M  サンプル.php
$ git commit
[master 88afe0e] Merge branch 'change_site'

これでマージ中のコンフリクトを正常に解決し、結果をコミットしました。


コマンドリファレンス

コマンド 説明 使用例
git branch ブランチの一覧表示、作成、削除を行います。ブランチの切り替えは行わず、ブランチの存在を管理するためのものです。 git branch:すべてのブランチを一覧表示
git branch new-branch:新しいブランチを作成
git branch -d old-branch:ブランチを削除
git checkout 指定したブランチに切り替えるか、作業ディレクトリ内のファイルを復元します。特定のコミットをチェックアウトするためにも使用できます。 git checkout branch-name:ブランチを切り替え
git checkout file.txt:ファイルを作業ディレクトリに復元
git checkout <commit-hash>:特定のコミットをチェックアウト
git switch ブランチの切り替え専用で、「git checkout」よりもシンプルで直感的であり、主にブランチ操作に使用されます。 git switch branch-name:指定したブランチに切り替え
git switch -c new-branch:新しいブランチを作成して切り替え
git merge 指定したブランチの変更を現在のブランチにマージします。 git merge branch-name:指定したブランチの変更を現在のブランチにマージ
git mergetool マージツールを起動して、マージコンフリクトを解決します。 git mergetool:デフォルトのマージツールでコンフリクトを解決
git mergetool --tool=<tool-name>:マージツールを指定
git log コミット履歴を表示します。 git log:コミット履歴を表示
git log --oneline:簡潔なモードでコミット履歴を表示
git stash 現在の作業ディレクトリ内の未コミットの変更を保存し、作業ディレクトリをクリーンな状態に戻します。 git stash:現在の変更を保存
git stash pop:最近保存した変更を復元
git stash list:保存されたすべての変更を一覧表示
git tag タグの作成、一覧表示、削除を行います。タグは特定のコミットをマークするために使用されます。 git tag:すべてのタグを一覧表示
git tag v1.0:新しいタグを作成
git tag -d v1.0:タグを削除
git worktree 1つのリポジトリ内で複数のワークツリーを扱うことができ、複数のブランチを同時に処理する場合に適しています。 git worktree add <path> branch-name:指定したパスに新しいワークツリーを追加し、指定したブランチに切り替え
git worktree remove <path>:ワークツリーを削除
その他の拡張