LoginSignup
6
9

More than 5 years have passed since last update.

rebase -iからcommitまで自動化する

Last updated at Posted at 2016-10-05

注: この記事で書いたことはvimscriptで頑張らなくてもgitの機能を使えばできます。

GIT_EDITOR=: git rebase -i --autosquash HEAD^^ のように GIT_EDITOR 環境変数に : を指定するとエディタの起動がスキップされます

指摘してくださったyuku_tさん、ありがとうございます。

rebase -iめんどくさい

こんな感じで、タイポ修正など小さな修正してrebaseするとき結構めんどくさいですよね。

rebase.gif

ですので自動化しました。

gitで起動するエディタはvimです。

~/.zprofile

function execIfCommandExists () {
  if type $1 2>/dev/null 1>/dev/null;then
    $1
  fi
}

# 現在の変更を1つ前のコミットと結合する
function gcommit-and-fixup(){
  git add .
  git commit --fixup=HEAD
  git rebase -i --autosquash HEAD^^
  execIfCommandExists tig
}

~/.vimrc

"gcommit-and-fixup用の設定
"自動で上書き保存して閉じる
function AutoSaveIfRebaseFixup()
  if match(getline(2), "fixup") == 0
    exec ":wq"
  endif
endfunction

if expand("%:t:r") == 'git-rebase-todo' && match(getline(2), "fixup")
  autocmd BufNewFile,BufRead git-rebase-todo call AutoSaveIfRebaseFixup()
endif

設定

git commit --fixup=HEADを使うとHEADのコミットメッセージの先頭にfixup!という文字列を追加したものが自動でコミットメッセージになります。
そしてgit rebase -i --autosquash HEAD^^は自動で対象コミットへのアクションをfixupにします。
スクリーンショット 2016-10-04 11.23.40.png

また、rebase -iで編集するファイル名はgit-rebase-todoですのでこのファイル名のファイルかつ2行目にfixupという文字列が含まれるもの

普段はgit commit --fixup=HEADを使わない人向けです。

gcommit-and-fixup.gif

gif作ってみたかったので割と張り切りました。

6
9
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9