git の操作は基本的に Emacs から Magit を使っているけど、ちょっとした操作とかはターミナルから git
コマンドを直接実行することもある。で、よく使うコマンドは git alias
でいい感じにしておくとタイプ数が少なくなったり、シュッと使えて体験がよくなる。
というわけで、僕の ~/.gitconfig
の alias
セクションにはこんな感じで設定している
[alias] aliases = config --get-regexp alias log-graph = log --graph --date=short --pretty=format:'%Cgreen%h %cd %Cblue%cn %Creset%s' log-all = log --graph --all --color --pretty='%x09%h %cn%x09%s %Cred%d%Creset' log-pr = !git log --stat --no-merges $( git merge-base origin/$( git config --get init.defaultBranch ) HEAD )...HEAD diff-pr = !git diff $( git merge-base origin/$( git config --get init.defaultBranch ) HEAD )...HEAD remotes = remote -v f = fetch origin --prune r = rebase s = status c = switch cd = !git switch $( git config --get init.defaultBranch ) co = checkout sui = submodule update --init pf = push --force-with-lease --force-if-includes wc = whatchanged ls = log --stat --no-merges lp = log -p
git config --get init.defaultBranch
というコマンドは git のデフォルトブランチを取得しているんだけど、現代ではリポジトリごとになることが普通だと思う。とくに新しく作業ブランチを作成する時にリポジトリごとに異なるデフォルトブランチを気にする必要なく、git cd
しておけば良いので、事故を防ぐことができて便利に使っている。
で、肝心のデフォルトブランチの設定は GitHub のリポジトリだったら、gh
コマンドからリポジトリに設定されているデフォルトブランチを gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name'
で取得できるので、以下のような関数を .zshrc
に登録している。
set-default-branch () { git rev-parse 2> /dev/null || return 0 defaultBranch=$( gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name' ) [ -z ${defaultBranch} ] && return 0 git config --local init.defaultBranch ${defaultBranch} echo "set init.defaultBranch : ${defaultBranch}" }