2012年3月10日

Git: 基本的なコマンド、その他

Yii Framework が Git, GitHub に移動した機会に Git を勉強しはじめました。今回はここ何日か触ってきた Git の基本的なコマンド、その他のリストになります。

作業の流れ

ローカルリポジトリの流れ
[Working Directory] --add--> [Index/Stage] --commit--> [HEAD]

ローカルリポジトリからリモートリポジトリへ、とその逆
[Local Repository] --push--> [Remote Repository]
[Local Repository] <--pull-- [Remote Repository]


HEADとは

最後にコミットした状態

HEADの1つ前のコミットした状態
HEAD^

HEADの2つ前のコミットした状態
HEAD^^

HEADの3つ前のコミットした状態
HEAD~3 or HEAD^^^


ステージング、コミット

addを取り消す
git reset HEAD <file_name>

addを省略してコミット (new file時は不可)
git commit -am "<commit_message>"

直前のコミットログを修正
git commit --amend (エディタ起動タイプ)
git commit --amend -m "<commit_message>" (直接書き直すタイプ)


タグ

タグの作成
git tag -a <tag_name> -m "<annotation>"

タグの一覧表示
git tag

タグの公開
git push origin <tag_name>
git push origin --tags (一括時)

ローカルのタグを削除
git tag -d <tag_name>

リモート先のタグを削除
git push origin :refs/tags/<tag_name>


ログ

最新のコミットを5件表示
git log -5


ブランチ

ローカルブランチの表示
git branch

すべてのブランチを表示 (ローカル、リモート)
git branch -a

ローカルブランチの作成
git branch <branch_name>

ローカルブランチ名の変更
git branch -m [<old_branch>] <new_branch>

ローカルブランチの切り替え
git checkout <branch_name>

ローカルブランチの作成と切り替えを同時の行う
git checkout -b <branch_name>

ローカルブランチの削除
git branch -d <branch_name>

マージしていないローカルブランチを強制的に削除
git branch -D <branch_name>

リモートブランチの削除
git push origin :<branch_name>

masterブランチにマージ
git checkout master
git merge <branch_name>

コンフリクトの解消
<<<<<<< ======= >>>>>>> などを除去後, addしてcommit
git mergetool (GUIでやる場合)

各ブランチの直近のコミットを表示
git branch -v

現在作業中のブランチにマージ済みか、そうでないかを調べる
git branch --merged
git branch --no-merged

ブランチのプッシュ
git push origin <branch_name>


リベース

公開リポジトリにプッシュしたコミットをリベースしてはいけない
リベースはあくまでもプッシュする前のコミットをきれいにするための方法
リベースするのはまだ公開していないコミットのみに限定する

リベースの最中にコンフリクトが発生した場合
マージ処理同様にコンフリクトを解決して、ステージング後、コミットではなく以下を行う
git rebase --continue

リベース前の状態に戻す
git rebase --abort

リベース後、masterブランチに戻ってマージすると
単純にマージしたときと違い Merge branch '<branch_name>' とは表示されないので
コミットの見栄えがきれいに保たれる


その他

コマンドのエイリアスを設定
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch

コミットを1行で表示
git config --global format.pretty oneline

gitk.diffの文字化け(日本語)を解消 (Windows)
git config --global gui.encoding utf-8


add, commit などの 取り消し に関しては、もう少し慎重に勉強していかないと、理解できない気がしました。


参考リンク

0 件のコメント:

コメントを投稿