The Git Stash command helps you store code that isn’t currently committed, for example when you encounter an online bug that needs to be fixed urgently, or when you accidentally type too many words on the keyboard. The latter can also discard all changes with Git checkout. In this case, discard without retrieving.

1. git stashWhat does it do? In what situation?

With git status, you will find that the current branch is very clean, and you can hardly see any changes. The code changes are also invisible, but they are actually temporarily saved. Execute the git Stash list and you’ll see that the staging area already has a record. At this point you can switch to another branch, fix the bug, and release. Then, when everything is settled, switch back to the branch you’re editing and continue with the functionality you left undone.

2. git stashAfter that, how do I restore the changed code?

Git Stash apply and you’ll find all your old code coming back as if it never happened. The next best thing to do is to remove the stash record from the staging area. To remove the most recent stash record, run the git Stash drop command. You can also use git stash pop instead of apply. The only difference between pop and apply is that pop not only restores the code for you, but also automatically removes the stash record so you don’t have to drop it again. Git stash pop = git stash apply + git stash drop

3. git stashCommands and examples:

(1) Git stash save “Save message” : When executing the storage, add a note for easy search. Only git Stash save can also be done, but it is not easy to identify when searching.

(2) Git Stash list: See what stash stores are. Each use of git Stash increases, and the latest index added is 0. (Feels like a stack, last in, first out)

By default, show the first stash. If you want to show other stash, add stash@{$num}. For example, the second git stash show stash@{1}. Outcomes, such as:

src/index.tsx |  30 ++++++-------
src/components/hello.tsx | 2++
2 files changed, 20 insertions(+),12 deletions(-)
Copy the code

This command is used to create a git diff file with one of the following stash shows: To display additional storage, run the git stash show stash@{$num} -p command, such as the second git stash show stash@{1} -p

The first stash is used by default, stash@{0}. If you want to use another stash, stash@{$num} is used. git stash apply stash@{1}

Delete the corresponding stash from the cache stack and apply the corresponding change to the current working directory. The default is the first stash, namely stash@{0}. If you want to apply and delete other stash files, run the following command: Git stash pop stash@{$num}, for example, apply and delete the second one: git stash pop stash@{1}

Git stash drop Stash @{$num} : Drop stash@{$num} from the list

Git Stash clear: Remove all cached stash files