Skip to content

Instantly share code, notes, and snippets.

@nhoriguchi
Created April 29, 2020 21:32
Show Gist options
  • Save nhoriguchi/95351ec53034744438a984cf057f360f to your computer and use it in GitHub Desktop.
Save nhoriguchi/95351ec53034744438a984cf057f360f to your computer and use it in GitHub Desktop.
cd $(dirname $BASH_SOURCE)
TMPD=$(mktemp -d)
cd $TMPD
demo() {
echo "$ $@"
eval $@
}
comment() {
echo "# $@"
}
demo "git init"
demo "git commit --allow-empty -m 'initial commit'"
demo "git status"
demo "date > file1"
demo "git add file1"
demo "git commit -m 'file commit'"
demo "date >> file1"
demo "git diff"
demo "git stash"
demo "git diff"
demo "echo string >> file1"
demo "echo string >> file1"
demo "git diff"
demo "git stash"
demo "git diff"
demo "git stash list"
comment "適当なコミットを作成し、複数の stash が存在する状況を作った。"
comment "任意の stash を取り出す。"
comment "引数なしだと最新 push した stash を取り出す。"
demo "git stash apply"
demo "git diff"
demo "git stash"
comment "再度 git stash list を見てみると、タイミングによっては stash が 3 つになることがあるが、これはたまたま stash した秒が異なっていたら、commit ID が変わって異なる stash とみなされてしまったためである。今たまたまスクリプトで高速に実行したため複数の stash が同一とみなされることがあるが、これは一般的にはまれなことである。実運用としては、apply/pop により stash は消えないことを留意しておけばよい。"
demo "git stash list"
comment "引数の指定方法 (-p で diff の中身まで見える。)"
demo git stash show -p 0
demo git stash show -p 1
demo "cat .git/refs/stash"
demo "git cat-file -t $(cat .git/refs/stash)"
comment "このハッシュのタイプは commit のようだ。"
comment "pop する先のコミットを変更してみる。"
demo "date >> file2"
demo "git add file2"
demo "git commit -m 'file2 added'"
demo "git log -n3 --oneline"
demo "git stash apply"
demo "git diff"
comment "無事 apply されているようだ。"
comment "もう一つの stash も apply してみる。"
demo "git stash apply 1"
demo "git diff"
comment "エラーが出てあとから apply しようとした方は apply されていないことが分かる。"
comment "いったん変更をキャンセルする。"
demo "git reset --hard"
comment "apply しようとしている stash がベースコミットとコンフリクトする場合は警告が出る。"
demo "date >> file1"
demo "git add file1"
demo "git commit -m 'file1 updated'"
demo "git stash apply"
demo "git diff"
comment "この場合は stash はコンフリクトした状態で適用される。"
demo "git stash list"
comment "stash の削除は pop サブコマンドを使用する。apply と pop はほぼ機能が同じだが、pop は適用された stash 状態を削除する。"
demo "git reset --hard"
demo "git checkout -b tmp HEAD~1"
demo "git stash pop 1"
demo "git diff"
demo "git stash list"
comment "確かに stash が一つ減っている。"
comment "全ての stash をクリアするには git stash clear を使用する。"
demo git stash clear
demo git stash list
comment cleanup
rm -rf $TMPD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment