Git tips

公開日:2024-10-20


はじめに

  • Gitを使っているときに便利なコマンドや設定をまとめておく。
  • 最終的にはdotfilesなどにまとめておけば良いかもしれない。

git commit時のeditor

  • vimへ変更する
    git config --global core.editor vim
    

Alias

  • git config でコマンドのエイリアスを設定できる

  • git config --global alias.<alias-name> "<command>" で設定する

  • 下記は.gitconfigでのalias例

    [alias]
        b = branch
        co = checkout
        ci = commit
        cm = commit -m
        st = status
        lo = log --oneline
        ad = add -A
        al = config --get-regexp ^alias\\.
    

commit template

  • .commit_template というファイルを作成し、コミットメッセージのテンプレートを記述することができる。

    # ==== Commit Messages ====
    
    # ==================== Format ====================
    # :emoji: Subject
    #
    # Commit body...
    # ==================== Emojis ====================
    # 🎉  :tada: 初めてのコミット(Initial Commit)
    # 🔖  :bookmark: バージョンタグ(Version Tag)
    # ✨  :sparkles: 新機能(New Feature)
    # 🐛  :bug: バグ修正(Bagfix)
    # ♻️  :recycle: リファクタリング(Refactoring)
    # 📚  :books: ドキュメント(Documentation)
    # 🎨  :art: デザインUI/UX(Accessibility)
    # 🐎  :horse: パフォーマンス(Performance)
    # 🔧  :wrench: ツール(Tooling)
    # 🚨  :rotating_light: テスト(Tests)
    # 💩  :hankey: 非推奨追加(Deprecation)
    # 🗑️  :wastebasket: 削除(Removal)
    # 🚧  :construction: WIP(Work In Progress)
    
  • 設定方法
    git config --global commit.template ~/.commit_template
    
  • 以降はgit commitを実行すると、上記のテンプレートが表示される。

OpenCommit

  • コミットメッセージを生成するツール(OpenAIでgit addしたファイルを解析し、コミットメッセージを生成してくれるので便利)

  • 事前にOpenAIのAPIキーを取得して、事前のクレジットを購入しておく

  • 以下のコマンドで設定を行う

    npm install -g opencommit
    oco config set OCO_API_KEY=<your_api_key>
    cat ~/.opencommit # APIキーが設定されていることを確認
    
  • その他の設定

    oco config set OCO_LANGUAGE=ja # 日本語に設定
    oco config set OCO_DESCRIPTION=false # コミットメッセージを詳細にする
    oco config set OCO_EMOJI=true # 絵文字を追加
    
  • 以下のコマンドでコミットメッセージが生成される

    git add -A
    oco
    

Husky

  • GitのHooksを設定できる
  • 下記のコマンドでインストールする
    npm install --save-dev husky
    npx husky init # 初期設定
    
  • 以下のコマンドでHooksを設定する
    vim .husky/pre-commit
    echo "npm run lint" >> .husky/pre-commit
    echo "npm run format" >> .husky/pre-commit
    
  • git commitを実行すると、pre-commitで設定したコマンドが実行される

参考


😄 END 😀