モヒカンメモ

髪色が定期的に変わることに定評のある(比較的)若者Webエンジニアの備忘録

名前をつけてgit stashする

gitには「今作業中なんだけどすぐに別の作業したくなって一時的に変更を退避させたいんだよねー」という時に使える git stash という便利コマンドがある。

gitを使い始めて6年ぐらいの先日、stashに名前 (メッセージ) をつけられることを知った。

git stashの構文

git stash save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [-u|--include-untracked] [-a|--all] [<message>]

使ってみる

前提

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

何も考えずstashすると...

$ git stash
Savd working directory and index state WIP on master: xxxxxxx いい感じの直前コミットのメッセージ

$ git status
On branch master
Your branch is up-to-date with 'origin/maser'.

nothing to commit, working tree clean

と変更の退避はできるものの、stashには「branch名」と「直前コミットのメッセージ」しか表示されないので時間が空いてからstash listを眺めるとそのstashが何を示すものかもう分からない。

$ git stash list
stash@{0}: WIP on master: xxxxxxx いい感じの直前コミットのメッセージ

僕もそうやっていくつものstashで 「良く分からんからgit stash pop😲 → ガリガリにコンフリクト😱 → これもう要らんやつや...😂」 という悲劇を起こした。

だがしかし! stashに名前をつけておくと出来るとほらこの通り。

$ git stash save "俺が考えた最強のREADME"
Saved working directory and index state On master: 俺が考えた最強のREADME
$ git stash list
stash@{0}: On master: 俺が考えた最強のREADME

そのstashが「俺が考えた最強のREADME (wip)」であることがひと目で分かって便利。

参考にした資料