Git-stash-operation

提供:Dev Guides
移動先:案内検索

Git-スタッシュ操作

製品に新しい機能を実装するとします。 コードが進行中で、突然顧客のエスカレーションが発生します。 このため、数時間は新しい機能の作業を脇に置いておく必要があります。 部分的なコードをコミットしたり、変更を破棄したりすることはできません。 そのため、部分的な変更を保存し、後でコミットするための一時スペースが必要です。

Gitでは、stash操作は変更された追跡ファイルを取得し、変更をステージングし、いつでも再適用できる未完成の変更のスタックに保存します。

[jerry@CentOS project]$ git status -s
M string.c
?? string

ここで、顧客のエスカレーションのためにブランチを切り替えたいが、まだ取り組んでいるものをコミットしたくない。そのため、変更を隠しておきます。 新しいスタッシュをスタックにプッシュするには、 git stash コマンドを実行します。

[jerry@CentOS project]$ git stash
Saved working directory and index state WIP on master: e86f062 Added my_strcpy function
HEAD is now at e86f062 Added my_strcpy function

これで、作業ディレクトリがクリーンになり、すべての変更がスタックに保存されます。 git status コマンドで確認しましょう。

[jerry@CentOS project]$ git status -s
?? string

これで、ブランチを安全に切り替えて他の場所で作業できます。 git stash list コマンドを使用して、隠された変更のリストを表示できます。

[jerry@CentOS project]$ git stash list
stash@{0}: WIP on master: e86f062 Added my_strcpy function

顧客のエスカレーションを解決し、半分完了したコードを探して新しい機能に戻ったとします。 git stash pop コマンドを実行して、スタックから変更を削除し、現在の作業ディレクトリに配置します。

[jerry@CentOS project]$ git status -s
?? string

[jerry@CentOS project]$ git stash pop

上記のコマンドは、次の結果を生成します。

# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
#
modified: string.c
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
#
string
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (36f79dfedae4ac20e2e8558830154bd6315e72d4)

[jerry@CentOS project]$ git status -s
M string.c
?? string