📌

私のgit alias

に公開

はじめに

自社勤務から客先常駐になったタイミングで、今まで使っていたGit Extensionsが使えなくなってしまいました。Git操作をすべてコマンドで行うようになり、よく使う処理をにalias(エイリアス)を整備しました。この記事では、私が実際に使っているGit aliasを紹介します。

alias

[alias]
    # グラフ表示のログ(全履歴)
    treeall = log --graph --all --pretty=format:'%Cred%h%Creset %C(yellow)%d%Creset %s  %C(bold blue)<%an>%Creset %Cgreen(%cd)' --abbrev-commit --date=format-local:'%Y/%m/%d %H:%M:%S'
   # グラフ表示のログ(現在のブランチ) 
    tree = log --graph --pretty=format:'%Cred%h%Creset %C(yellow)%d%Creset %s  %C(bold blue)<%an>%Creset %Cgreen(%cd)' --abbrev-commit --date=format-local:'%Y/%m/%d %H:%M:%S'
    # developブランチにチェックアウト
    dev = checkout develop
    # ステージングエリアから変更を戻す
    unstage = reset HEAD
    # 作業ディレクトリの変更をリセット
    resetf = checkout .
    # 現在のブランチをpush
    pushc  = push origin HEAD
    # 現在のブランチを強制push
    pushf = push --force-with-lease origin HEAD
    # リモートとローカルの全てのブランチを表示
    ball = branch -a
    # ローカルブランチを表示
    b = branch
    # スタッシュのリストを表示
    sl = stash list

コマンド説明

グラフ表示のログ(全履歴)

git log --graph --all --pretty=format:'%Cred%h%Creset %C(yellow)%d%Creset %s  %C(bold blue)<%an>%Creset %Cgreen(%cd)' --abbrev-commit --date=format-local:'%Y/%m/%d %H:%M:%S'

グラフは以下のように表示されます。

現在のブランチをpush

git push origin HEAD

上記のコマンドは以下のコマンドと同じ意味です。
現在、developブランチにいるとします。

git push origin develop

現在のブランチを強制push

git push --force-with-lease origin HEAD

通常のgit push -fではなく--force-with-leaseにすることで、他の人がプッシュしていた場合にプッシュができないようになります。

--force-with-leaseを使うことで、他の人がすでにプッシュした場合、誤って上書きするリスクを避けることができるので私はこちらを使用しています。

Discussion