Git stash

LHT

Stashing Changes: git stash

Application Scenarios

  1. When you are developing a project on the dev branch and a bug arises that requires immediate fixing, but your current work is only half done and you don’t want to commit it yet. You can use the git stash command to save your changes to the stash, then switch to the hotfix branch to fix the bug. After completing the fix, you can switch back to the dev branch and restore your stashed changes.

  2. Due to an oversight, you developed content on the master branch instead of the dev branch. You can stash your changes, switch back to the dev branch, and then restore your content.

In summary, the git stash command allows you to save changes that you don’t want to commit yet to a stash. You can later restore these stashed changes to any branch, not just the one you were originally working on. The stash can include changes in both the working directory and the staging area, meaning any uncommitted changes will be saved.

Command Details

1. git stash

This command saves all uncommitted changes (both in the working directory and staging area) to the stash for later restoration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git status
On branch 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: src/main/java/com/wy/CacheTest.java
modified: src/main/java/com/wy/StringTest.java

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

$ git stash
Saved working directory and index state WIP on master: b2f489c second

$ git status
On branch master
nothing to commit, working tree clean

2. git stash save

This command functions the same as git stash, but it allows you to add a message for reference. For example, git stash results in:

1
stash@{0}: WIP on master: b2f489c second

While git stash save "test1" results in:

1
stash@{0}: On master: test1

3. git stash list

This command displays the contents currently stored in the stash.

4. git stash pop

This command pops the most recent stash entry and applies it to the working directory of the current branch. Note that this command deletes the popped stash entry (it follows a last-in, first-out order). For instance, after executing git stash save "test1" and git stash save "test2":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git stash list
stash@{0}: On master: test2
stash@{1}: On master: test1

$ git stash pop
On branch 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: src/main/java/com/wy/StringTest.java

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (afc530377eacd4e80552d7ab1dad7234edf0145d)

$ git stash list
stash@{0}: On master: test1

This shows that test2 was popped first. If there are conflicts between the restored changes and the current working directory (e.g., both modified the same lines), an error will occur, and you need to resolve the conflict, potentially by creating a new branch.

5. git stash apply

This command applies the changes from the stash to the working directory without deleting the stash entry. This means you can apply the same stash entry multiple times across different branches.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git stash apply
On branch 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: src/main/java/com/wy/StringTest.java

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

$ git stash list
stash@{0}: On master: test2
stash@{1}: On master: test1

The stash contents are still available. You can specify which stash to apply using git stash apply <stash-name> (e.g., stash@{1}).

6. git stash drop <name>

This command removes a specific stash entry from the stash.

7. git stash clear

This command clears all entries in the stash.

8. git stash show

This command shows the differences between the latest stash and the current directory.

1
2
3
$ git stash show
src/main/java/com/wy/StringTest.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

To view the differences for a specific stash, use git stash show stash@{1}. You can also use git stash show -p for a more detailed view:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ git stash show -p
diff --git a/src/main/java/com/wy/CacheTest.java b/src/main/java/com/wy/CacheTest.java
index 6e90837..de0e47b 100644
--- a/src/main/java/com/wy/CacheTest.java
+++ b/src/main/java/com/wy/CacheTest.java
@@ -7,6 +7,6 @@ package com.wy;
*/
public class CacheTest {
public static void main(String[] args) {
- System.out.println("git stash test");
+ System.out.println("git stash test1");
}
}
diff --git a/src/main/java/com/wy/StringTest.java b/src/main/java/com/wy/StringTest.java
index a7e146c..711d63f 100644
--- a/src/main/java/com/wy/StringTest.java
+++ b/src/main/java/com/wy/StringTest.java
@@ -12,7 +12,7 @@ public class StringTest {

@Test
public void test1() {
- System.out.println("=================");
+ System.out.println("git stash test1");
System.out.println(Strings.isNullOrEmpty(""));//true
System.out.println(Strings.isNullOrEmpty(" "));//false
System.out.println(Strings.nullToEmpty(null));//""

Similarly, you can view the differences for a specific stash with git stash show stash@{1} -p.

9. git stash branch

This command creates a new branch from the latest stash. This is useful when you have stashed changes but want to continue developing on the current branch and later restore the stash content to the working directory. If modifications conflict (even if not on the same line), creating a new branch can help resolve these conflicts. You will need to resolve any conflicts manually.

  • Title: Git stash
  • Author: LHT
  • Created at : 2022-06-03 06:10:00
  • Link: https://blog.327774.xyz/2022/06/02/git/Git stash/
  • License: This work is licensed under CC BY-NC-SA 4.0.