Use scenarios:
1. When a project is being developed on the dev branch, a bug appears in the project that needs urgent repair, but the content being developed is only half completed and does not want to be submitted. You can save the modified code content to the stack area using the git stash command.Then switch to the hotfix branch to fix the bug, and after fixing, cut back to the dev branch again to restore the just saved content from the stack.(
2. Due to negligence, content that should have been developed in the dev branch has been developed on the master, and needs to be cut back to the dev branch for development. You can use git stash to save the content on the stack.Once you have cut back to the dev branch, you can restore the content again.(
Overall, the git stash command is designed to save changes that you do not want to submit at this time to the stack, which can then be restored on a branch.
1. git stash
Save all uncommitted changes to the stack for subsequent recovery of the current working directory.If you want to add a comment, you can use git stash save "hotfix...".
$ 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 list
View the contents of the current stash staging area
stash@{0}: On master: test2
3. git stash pop
Pop up the contents of the current stash and apply them to the working directory corresponding to the current branch.(
Note: This command deletes the most recently saved content in the stack (stack is FIFO)
$ 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
4. git stash apply
Apply the contents of the stack to the current directory, unlike git stash pop, this command does not delete the contents from the stack, which means it can apply the contents of the stack to the working directory multiple times to accommodate multi-branching situations.You can use the git stash apply + stash name (such as stash@{test1}) to specify which stash is restored to the current working directory.
$ 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
5. git stash drop + stash name
Remove a specified stash from the stash stack
6. git stash clear
Clear everything from the stash stack
7. git stash show
View the differences between the most recently saved stash in the stack and the current directory.
$ git stash show src/main/java/com/wy/StringTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
8. git stash show stash@{1} -p
View details of the differences between the specified stash and the local directory
finish