【Git】Git ~ 基本編 / ブランチ ~

■ はじめに

Gitのブランチについて、学ぶ

目次

コマンド一覧

【1】ブランチの一覧表示
【2】ブランチ作成
【3】ブランチへ移動
【4】ブランチを取り込む
【5】マージ済/未処理のブランチ一覧を表示
【6】ブランチの削除
【7】変更の状態を確認

応用

【AA】リモートブランチ名を変更するには...

 【1】ブランチの一覧表示

 * 「git branch」で一覧表示

 コマンド例

git branch
* master
 => 「*」が、現在いる位置

【2】ブランチ作成

 * 「git branch [ブランチ名]」で作成

 コマンド例

git branch first_branch

# 作成できているかどうか確認
git branch
  first_branch
* master
 => 「*」が、現在いる位置

【3】ブランチへ移動

 * 「git checkout [ブランチ名]」で移動
 * 「git checkout -b [ブランチ名]」だと、作成し移動

 コマンド例

git checkout first_branch

# 作成できているかどうか確認
git branch
* first_branch
  master
 => 「*」が、現在いる位置。「first_branch」に移動できている。

【4】ブランチを取り込む

 * 「git merge [ブランチ名]」で別のブランチに取り込む

 コマンド例

git checkout first_branch

# 追加するファイルを作る
vi script.js
~~~~~~
alert('Hello World!');
~~~~~~

# 追加
git add .

# コミット
git commit -m "To add script"

# マスターに戻る
git checkout master

# 「ls」したら、「script.js」はない
ls

# ★ここ注目:ブランチ「first_branch」をマスターに反映★
git merge first_branch

# 「ls」したら、「script.js」が反映
ls

【5】マージ済/未処理のブランチ一覧を表示

* 「git branch --merged」でマージ済みのブランチ一覧を表示
* 「git branch --no-merged」でマージしていないbranch一覧を表示

 コマンド例

git checkout master

# マージ済みのブランチ一覧を表示
git branch --merged

# マージしていないbranch一覧を表示
git branch --no-merged

【6】ブランチの削除

構文

git branch -d [ブランチ名]

 コマンド例

git branch -d first_branch

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

構文

git push --delete origin [ブランチ名]

【7】変更の状態を確認

 * 「git status」で変更の状態を確認

 コマンド例

コンフリクト起こしている場合

git status

On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:   index.html

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

関連記事

Git ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2018/06/29/104028
Git ~ ブランチあれこれ ~
https://dk521123.hatenablog.com/entry/2022/07/10/000000