LoginSignup
0
0

More than 1 year has passed since last update.

[Git] 動作を試す 実行例17:stashで作業フォルダ内の変更を一時退避する

Posted at

Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。

1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。

※前回記事のリポジトリ状態からの続きになっています。

前回記事へ 目次へ:Git関連記事のまとめページ 次回記事へ

実行例

git: git stashの使い方 - Qiita
https://qiita.com/keisuke0508/items/4ad7caf544b1ad631fd7

引用:
変更差分をコミットせずに一時的に退避させることで保存できる。
作業中に他のブランチでの作業が必要になったときなどに便利。
「git stash」変更差分を退避させる。untracked fileは退避されない。
「git stash -u」untracked fileも含めて変更差分を退避させる。

(※ひとまず適当に変更の操作)
echo Sample-Added-15 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
Sample-Added-15

dir /b
↓
結果: 
test1.txt
test2.txt
test4.txt

echo Sample-Added-71 > test7.txt
del test4.txt
dir /b
↓
結果: 
test1.txt
test2.txt
test7.txt

git status
↓
結果: 
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test1.txt
        deleted:    test4.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test7.txt

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

-----

※変更差分を退避させる
git stash
↓
結果: 
Saved working directory and index state WIP on master: 94996a0 message4

git status
↓
結果: 
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test7.txt

nothing added to commit but untracked files present (use "git add" to track)

dir /b
↓
結果: 
test1.txt
test2.txt
test4.txt
test7.txt

-----

git stash -u
↓
結果: 
Saved working directory and index state WIP on master: 94996a0 message4

git status
↓
結果: 
On branch master
nothing to commit, working tree clean

git stash list
↓
結果: 
stash@{0}: WIP on master: 94996a0 message4
stash@{1}: WIP on master: 94996a0 message4

dir /b
↓
結果: 
test1.txt
test2.txt
test4.txt

-----

git stash apply 0
↓
結果: 
Already up to date.
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test7.txt

nothing added to commit but untracked files present (use "git add" to track)

git stash apply 1
↓
結果: 
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test1.txt
        deleted:    test4.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test7.txt

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

dir /b
↓
結果: 
test1.txt
test2.txt
test7.txt

-----

git stash -u -m "stash1"
↓
結果: 
Saved working directory and index state On master: stash1

dir /b
↓
結果: 
test1.txt
test2.txt
test4.txt

git status
↓
結果: 
On branch master
nothing to commit, working tree clean

git stash list
↓
結果: 
stash@{0}: On master: stash1
stash@{1}: WIP on master: 94996a0 message4
stash@{2}: WIP on master: 94996a0 message4

-----

git stash drop 1
↓
結果: 
Dropped refs/stash@{1} (b4f0c3a4dde49d9af756b7667583fc0c9e98af44)

git stash drop 1
↓
結果: 
Dropped refs/stash@{1} (2f264024a0b7b3b1522da4ec3e78d2d9bed5fdb1)

git stash list
↓
結果: 
stash@{0}: On master: stash1

-----

※内容を見る。
git stash show 0
↓
結果: 
 test1.txt | 2 ++
 test4.txt | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)

git stash show 0 -p
↓
結果: 
diff --git a/test1.txt b/test1.txt
index bb7eb1a..0b705c8 100644
--- a/test1.txt
+++ b/test1.txt
@@ -2,3 +2,5 @@ Sample-Added-11
 Sample-Added-12
 Sample-Added-13
 Sample-Added-14
+Sample-Added-15
diff --git a/test4.txt b/test4.txt
deleted file mode 100644
index d42c59e..0000000
--- a/test4.txt
+++ /dev/null
@@ -1 +0,0 @@
-Sample-Added-41

git stash show 0 -p -U1
↓
結果: 
diff --git a/test1.txt b/test1.txt
index bb7eb1a..0b705c8 100644
--- a/test1.txt
+++ b/test1.txt
@@ -4 +4,3 @@ Sample-Added-13
 Sample-Added-14
+Sample-Added-15
diff --git a/test4.txt b/test4.txt
deleted file mode 100644
index d42c59e..0000000
--- a/test4.txt
+++ /dev/null
@@ -1 +0,0 @@
-Sample-Added-41

※untracked状態のtest7.txtは、showの中に表示されない模様。

環境

Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。

0
0
0

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
0
0