LoginSignup
43
40

More than 5 years have passed since last update.

git commit --fixup で fixup する対象を peco/fzf で選べるスクリプト書いた

Last updated at Posted at 2015-10-19

fixup-interactive.gif

git commit --fixup が何かについてはgit commit --fixup とは何か - 詩と創作・思索のひろばを読んでもらうとして、 fixup を適用したいコミットをいちいち git log で調べるのが面倒なのでインタラクティブに選べるようにした。

以下のスクリプトをパスの通ったディレクトリに置くと git fixup が使えるようになる。適当な変更を git add して git fixup を実行すると、その変更を fixup として適用したいコミットを peco や fzf で選べる。

git-fixup
#!/bin/bash

FILTER=${FILTER:-peco}
MAX_LOG_COUNT=${MAX_LOG_COUNT:-30}

if git diff --cached --quiet; then
    commits="No staged changes. Use git add -p to add them."
    ret=1
else
    commits=$(git log --oneline -n "$MAX_LOG_COUNT")
    ret=$?
fi

if [[ "$ret" != 0 ]]; then
    headline=$(head -n1 <<< "$commits")
    if [[ "$headline" = "No staged changes. Use git add -p to add them." ]]; then
        echo "$headline" >&2
    fi
    exit "$ret"
fi

line=$("$FILTER" <<< "$commits")
[[ "$?" = 0 && "$line" != "" ]] || exit "$?"

git commit --fixup "$(awk '{print $1}' <<< "$line")" "$@"
43
40
4

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
43
40