Git branch ======== {{tag: git}} git の branch は使い勝手が超絶良い -------- {{TOC 3-}} ### Git の branch ### [[/linux/git|Git]] のブランチは作成・削除・切り替えが素早く行えて使い勝手が良い。 merge 作業も簡単で、これは使わないと損。 `git branch -h` で全体像が拝める。 $ git branch -h usage: git branch [options] [-r | -a] [--merged | --no-merged] or: git branch [options] [-l] [-f] [] or: git branch [options] [-r] (-d | -D) or: git branch [options] (-m | -M) [] Generic options -v, --verbose be verbose -t, --track set up tracking mode (see git-pull(1)) --set-upstream change upstream info --color[=] use colored output -r act on remote-tracking branches --contains print only branches that contain the commit --abbrev[=] use digits to display SHA-1s Specific git-branch actions: -a list both remote-tracking and local branches -d delete fully merged branch -D delete branch (even if not merged) -m move/rename a branch and its reflog -M move/rename a branch, even if target exists -l create the branch's reflog -f, --force force creation (when already exists) --no-merged print only not merged branches --merged print only merged branches ### branch の一覧 ### #### 全ブランチ #### $ git branch -a #### 手元のブランチだけ #### $ git branch #### リモートのブランチだけ #### $ git branch -r #### ブランチの詳細 #### $ git branch -v ### 新しい branch の作成 ### $ git branch [BRANCH_NAME] ### branch の切り替え ### $ git checkout [BRANCH_NAME] ### branch を作成してすぐ切り替え ### $ git checkout -b [BRANCH_NAME] ### branch の削除 ### $ git branch -d [BRANCH_NAME] merge していない内容があっても、とにかくブランチを削除する。 $ git branch -D [BRANCH_NAME] リモートブランチの削除は push で行う $ git push origin :[BRANCH_NAME] ### 削除してよい branch の一覧 ### マージ済みのブランチを一覧する $ git branch --merged ### git stash ### `git checkout` で branch を切り替えようとしたとき、コミットしていないと怒られて切り替えることができない。 そんなとき役に立つのが `git stash` 現在の作業内容をスタックに保存して、ワーキングコピーを一時的に HEAD な状態に戻せる。 他のブランチに移動して作業をして戻ってきたり、現在の作業をいったんとりやめて別のアプローチを試して、やっぱ今の状態に戻りたい、とかいったことが気楽にできる。 #### 現在の作業内容を保存する #### $ git stash #### stash で保存している一覧 #### $ git stash list #### stash で保存した状態に戻す #### $ git stash pop #### stash を削除する #### $ git stash drop stash の内容をすべて削除するなら clear $ git stash clear ### .bashrc に br alias ### .bashrc に br エイリアス作っておくと便利。 alias br='git branch -a' ### 差分の取り込み ### ブランチの差分を取り込むには merge コマンドを使用する。 $ git merge [BRANCH_NAME] merge は、カレントのブランチに、対象の [BRANCH_NAME] の作業分がマージされる。