- ローカルブランチを作成
- リモートにブランチを送る
- リモートにブランチをローカルにチェックアウトする
- ローカルブランチを削除
- リモートブランチを削除
- 独立ブランチ(Orphan Branch)の作成
- 空ブランチ
- ローカル、リモートブランチの変更
ローカルブランチを作成
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
マスターブランチだけある状態からスタート。
git checkout -b <branch>:
$ git checkout -b aaa
でaaaブランチを作成。
$ git branch -a
master
* aaa
remotes/origin/HEAD -> origin/master
remotes/origin/master
リモートにブランチを送る
git push origin <branch>:
$ git push origin aaa
で
$ git branch -a
master
* aaa
remotes/origin/HEAD -> origin/master
remotes/origin/aaa
remotes/origin/master
GitHub等に送ってる場合はチェックするとaaaが出来てるはず。
リモートにブランチをローカルにチェックアウトする
$ git checkout -b aaa origin/aaa
ローカルブランチを削除
aaaを消すために一旦masterに戻る(aaaに居るとaaaが消せないので)。
$ git checkout master
git branch -D <branch>:
$ git branch -D aaa
で
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/aaa
remotes/origin/master
消す際に-dと-Dのオプションがあって、
-dだとまだHEADにマージされてないコミットが
ある場合はエラーが出ます。
-Dだと有無をいわさず削除します。
リモートブランチを削除
git push --delete origin <branch>
$ git push --delete origin aaa
または
git push origin :<branch>
$ git push origin :aaa
で
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
元通りローカル、リモート両方共からaaaを削除。
独立ブランチ(Orphan Branch)の作成
checkout時に--orphanオプションを使うと1
元になるブランチの履歴を全て消し去った状態で
新しいブランチを作る事が出来ます。
試しに空のレポジトリを作りテスト。
$ mkdir testrepo;cd testrepo;cd testrepo
$ git init
$ git checkout -b master
$ touch file1
$ git add file1
$ git commit -m "first import: file1"
$ vi file1 # write something like "updated"...
$ git commit -am "updated file1"
$ git branch
* master
$ ls
file1
$ git log
commit 3116384853c268088a5d01f4e1230a3979b0ad38
Author: rcmdnk <[email protected]>
Date: Wed Oct xx xx:02:00 2013 +xxxx
updated file1
commit 6e63ec61a0446b16cbeb66e4d5b78df191e7bcf2
Author: rcmdnk <[email protected]>
Date: Wed Oct xx xx:01:00 2013 +xxxx
first import: file1
こんな状態を作っておく。
普通のブランチaaaを作ると
$ git checkout -b aaa
$ git status
# On branch aaa
nothing to commit, working directory clean
$ git log
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: rcmdnk <[email protected]>
Date: Wed Oct xx xx:02:00 2013 +xxxx
updated file1
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: rcmdnk <[email protected]>
Date: Wed Oct xx xx:01:00 2013 +xxxx
first import: file1
$ vi file1 # write something like "updated in aaa"...
$ git commit -am "updated file1 in aaa"
$ git log
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: rcmdnk <[email protected]>
Date: Wed Oct xx xx:03:00 2013 +xxxx
updated file1 in aaa
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: rcmdnk <[email protected]>
Date: Wed Oct xx xx:02:00 2013 +xxxx
updated file1
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: rcmdnk <[email protected]>
Date: Wed Oct xx xx:01:00 2013 +xxxx
first import: file1
こんな感じでmasterの履歴の続きで履歴が作成されます。
一方、独立ブランチだと
$ git checkout master
$ git checkout --orphan bbb
$ git status
# On branch bbb
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: file1
#
$ git log
fatal: bad default revision 'HEAD'
$ git branch
aaa
master
$ git ci -am "first at bbb"
$ git log
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: rcmdnk <[email protected]>
Date: Thu Oct xx xx:04:00 2013 +xxxx
first at bbb
の様に、masterにあったfileは引き継がれますが、
それも新たに加えられたファイルの様になります(ただしすでにトラックはされてる状態)。
この状態ではbbbブランチには何も書き込まれてない状態なので、
改めてaddしてcommitしてようやくbbbブランチが
上の様にbranchコマンドなどで認識される様になります。
show-branchしてみると
$ git checkout master
$ git show-branch
! [aaa] updated file1 in aaa
! [bbb] first at bbb
* [master] updated
---
+ [bbb] first at bbb
+ [aaa] updated file1 in aaa
+ * [master] updated file1
+ * [master^] first import: file1
こんな感じで確かにbbbはmasterとの関係が切れてる
(下の方でmasterの行においてbbbに当たる部分(真ん中)に+が無い)
ことがわかります。
空ブランチ
独立ブランチを一歩推し進めて、単に最初に全部消してしまうだけです。
$ git checkout --orphan ccc
$ git rm -rf .
$ # add new file/directory, etc...
これで履歴も無ければファイルもディレクトリも無いブランチになるので まっさらな状態からのスタートに出来ます。
ローカル、リモートブランチの変更
-
古いバージョンのGitだとこのオプジョンが無くて、
.gitにあるindexを消したりクリーンアップしたり 手作業で行う必要がありました: GithubのProject Pagesを作る手順を調べてみた。もしくはgitで空ブランチを作る方法を調べてみた。 ↩
