Git-managing-branches
Git-ブランチの管理
分岐操作により、別の開発ラインを作成できます。 この操作を使用して、開発プロセスを2つの異なる方向に分岐できます。 たとえば、6.0バージョンの製品をリリースし、7.0の機能の開発を6.0のバグ修正とは別に維持できるようにブランチを作成する場合があります。
ブランチを作成
トムは、git branch <branch name>コマンドを使用して新しいブランチを作成します。 既存のブランチから新しいブランチを作成できます。 特定のコミットまたはタグを開始点として使用できます。 特定のコミットIDが提供されていない場合、開始点としてHEADを使用してブランチが作成されます。
[jerry@CentOS src]$ git branch new_branch
[jerry@CentOS src]$ git branch
* master
new_branch
新しいブランチが作成されます。 Tomはgit branchコマンドを使用して、利用可能なブランチを一覧表示しました。 Gitは、現在チェックアウトされているブランチの前にアスタリスクマークを表示します。
ブランチ作成操作の図解は以下に示されています-
ブランチを切り替える
Jerryはgit checkoutコマンドを使用してブランチを切り替えます。
[jerry@CentOS src]$ git checkout new_branch
Switched to branch 'new_branch'
[jerry@CentOS src]$ git branch
master
* new_branch
ブランチを作成して切り替えるためのショートカット
上記の例では、2つのコマンドを使用してそれぞれブランチを作成および切り替えています。 Gitはcheckoutコマンドで –b オプションを提供します。この操作により、新しいブランチが作成され、すぐに新しいブランチに切り替わります。
[jerry@CentOS src]$ git checkout -b test_branch
Switched to a new branch 'test_branch'
[jerry@CentOS src]$ git branch
master
new_branch
* test_branch
ブランチを削除する
ブランチを削除するには、git branchコマンドで-Dオプションを指定します。 ただし、既存のブランチを削除する前に、他のブランチに切り替えてください。
Jerryは現在 test_branch にいるので、そのブランチを削除したいと考えています。 そこで、彼は以下に示すようにブランチを切り替えてブランチを削除します。
[jerry@CentOS src]$ git branch
master
new_branch
* test_branch
[jerry@CentOS src]$ git checkout master
Switched to branch 'master'
[jerry@CentOS src]$ git branch -D test_branch
Deleted branch test_branch (was 5776472).
これで、Gitは2つのブランチのみを表示します。
[jerry@CentOS src]$ git branch
* master
new_branch
ブランチの名前を変更する
Jerryは、文字列操作プロジェクトでワイド文字のサポートを追加することにしました。 彼はすでに新しいブランチを作成していますが、ブランチ名は適切ではありません。 そのため、 – m オプションの後に* oldブランチ名*と* newブランチ名*を使用してブランチ名を変更します。
[jerry@CentOS src]$ git branch
* master
new_branch
[jerry@CentOS src]$ git branch -m new_branch wchar_support
これで、git branchコマンドは新しいブランチ名を表示します。
[jerry@CentOS src]$ git branch
* master
wchar_support
2つのブランチをマージする
Jerryは、ワイド文字列の文字列長を返す関数を実装しています。 新しいコードは次のように表示されます-
[jerry@CentOS src]$ git branch
master
*wchar_support
[jerry@CentOS src]$ pwd
/home/jerry/jerry_repo/project/src
[jerry@CentOS src]$ git diff
上記のコマンドは、次の結果を生成します-
t a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..8fb4b00 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,4 +1,14 @@
#include <stdio.h>
+#include <wchar.h>
+size_t w_strlen(const wchar_t* s)
{
const wchar_t *p = s;
while (*p)
+ ++p;
+ return (p - s);
}
テスト後、彼は変更をコミットして新しいブランチにプッシュします。
[jerry@CentOS src]$ git status -s
M string_operations.c
?? string_operations
[jerry@CentOS src]$ git add string_operations.c
[jerry@CentOS src]$ git commit -m 'Added w_strlen function to return string lenght of wchar_t
string'
[wchar_support 64192f9] Added w_strlen function to return string lenght of wchar_t string
1 files changed, 10 insertions(+), 0 deletions(-)
Jerryはこれらの変更を新しいブランチにプッシュしているため、 master ブランチの代わりにブランチ名 wchar_support を使用したことに注意してください。
[jerry@CentOS src]$ git push origin wchar_support <−−− Observer branch_name
上記のコマンドは、次の結果を生成します。
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 507 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
To [email protected]:project.git
* [new branch]
wchar_support -> wchar_support
変更をコミットした後、新しいブランチは次のように表示されます-
トムは、ジェリーがプライベートブランチで何をしているのか興味があり、 wchar_support ブランチのログをチェックします。
[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src
[tom@CentOS src]$ git log origin/wchar_support -2
上記のコマンドは、次の結果を生成します。
commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3
Author: Jerry Mouse <[email protected]>
Date: Wed Sep 11 16:10:06 2013 +0530
Added w_strlen function to return string lenght of wchar_t string
commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 10:21:20 2013 +0530
Removed executable binary
コミットメッセージを表示することにより、トムはジェリーがワイドキャラクター用のstrlen関数を実装し、マスターブランチで同じ機能が必要であることを認識しています。 再実装する代わりに、彼はブランチをマスターブランチにマージして、ジェリーのコードを取ることにしました。
[tom@CentOS project]$ git branch
* master
[tom@CentOS project]$ pwd
/home/tom/top_repo/project
[tom@CentOS project]$ git merge origin/wchar_support
Updating 5776472..64192f9
Fast-forward
src/string_operations.c | 10 ++++++++++
1 files changed, 10 insertions(+), 0 deletions(-)
マージ操作の後、マスターブランチは次のように表示されます-
これで、ブランチ wchar_support がmasterブランチにマージされました。 コミットメッセージを表示するか、string_operation.cファイルに加えられた変更を表示することで確認できます。
[tom@CentOS project]$ cd src/
[tom@CentOS src]$ git log -1
commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3
Author: Jerry Mouse
Date: Wed Sep 11 16:10:06 2013 +0530
Added w_strlen function to return string lenght of wchar_t string
[tom@CentOS src]$ head -12 string_operations.c
上記のコマンドは、次の結果を生成します。
#include <stdio.h>
#include <wchar.h>
size_t w_strlen(const wchar_t *s)
{
const wchar_t *p = s;
while (*p)
++p;
return (p - s);
}
テスト後、彼はコードの変更をmasterブランチにプッシュします。
[tom@CentOS src]$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:project.git
5776472..64192f9 master −> master
ブランチのリベース
Git rebaseコマンドはブランチマージコマンドですが、違いはコミットの順序を変更することです。
Git mergeコマンドは、他のブランチからのコミットを現在のローカルブランチのHEADの上に配置しようとします。 たとえば、ローカルブランチにコミットA→ B→ C→ Dがあり、マージブランチにコミットA→ B→ X→ Yがある場合、git mergeは現在のローカルブランチをA→のようなものに変換しますB→ C→ D→ X→ Y
Git rebaseコマンドは、現在のローカルブランチとマージブランチ間の共通の祖先を見つけようとします。 次に、現在のローカルブランチのコミットの順序を変更して、コミットをローカルブランチにプッシュします。 たとえば、ローカルブランチにコミットA→ B→ C→ Dがあり、マージブランチにコミットA→ B→ X→ Yがある場合、Gitリベースは現在のローカルブランチをA-のようなものに変換します> B→ X→ Y→ C→ D。
複数の開発者が単一のリモートリポジトリで作業する場合、リモートリポジトリ内のコミットの順序を変更することはできません。 この状況では、リベース操作を使用して、ローカルコミットをリモートリポジトリコミットの上に配置し、これらの変更をプッシュできます。