Git の運用

手元の設定と日々の操作。ブランチ戦略の背景 (なぜトランクベースなのか、開発ブランチをなぜ使わないか)は バージョン管理 にある。

ブランチの運用基準

開発・機能・リリース用ブランチは作成しない

差分 or ブランチの生存期間は数時間以内にする
トランクが更新されてから各開発者のトランクが最新になるまでの時間は数時間以内にする
コンフリクトを発生させない
マージと再テストをさせない
レビュー負担、レビューミスを減らす

コードロック or コードフリーズ期間を作らない

コミット前には各種チェックが動作(数分以内)して、問題ないときだけコミットすることができる
ビルドとテストプロセスは数分以内

SSH

SSH キーの作成

  • email は仕事用とプライベート用は分ける
ssh-keygen -t ed25519 -C "${email}"

.ssh/config

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

SSH キーを ssh-agent に追加

eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_ed25519

SSH キーの GitHub への追加

New SSH Key

Title: ${host}/${user}/.ssh/id_ed25519.pub
Key: cat ~/.ssh/id_ed25519.pubの内容

~/.gitconfig

[user]
        name =
        email =
[color]
        ui = true
[alias]
        st = status
        co = checkout
        br = branch
        diffs = diff --staged
        rmc = rm --cached
[merge]
        ff = only
        tool = sublime
[pull]
        rebase = true
[init]
        defaultBranch = main
[url "git@github.com:"]
        insteadOf = https://github.com/
[mergetool "sublime"]
        cmd = subl -n -w $MERGED
[core]
        editor = subl -n -w
git config --global user.name ""
git config --global user.email ""
 
git config --global color.ui true
 
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
git config --global core.editor 'subl -n -w'
 
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.diffs 'diff --staged'
git config --global alias.rmc "rm --cached"
 
git config --global pull.rebase true
 
git config --global merge.ff only
git config --global merge.tool sublime
git config --global mergetool.sublime.cmd "subl -n -w \$MERGED"
 
git config --global init.defaultBranch main

日々の操作

クローン

git clone git@github.com:${user}/${repo}.git

作業ブランチ作成

git branch -r
git co -b develop|main origin/develop|main
git co -b ${branch} develop|main

確認

git st
git diff
git diffs

コミット

git add .
git commit -m "コミットメッセージ" | git commit -v
  • revertすることを考えて細かく、複数の変更を1コミットに混ぜない

差分の一時退避・復元

git stash
git stash pop

プッシュ前

git co develop|main
git pull -p
git co ${branch}
git rebase|merge develop|main

プッシュ

git push origin ${branch}

掃除

git gc
git gc --aggressive

コミット履歴

  • コミット履歴を見れば、過去にどういう案件でどういう変更があったか分かるようにする
  • 適宜Squashして見やすくしてからプルリクエストを作成する

GitHub のプルリクのマージ時

  • 差分をApproveする
  • Squash mergeにする
  • コミットメッセージはすべて消し、プルリクのタイトル(案件・修正名と該当のチケットのURL)だけにする

関連