git stash saves and restores work progress

Application scenarios:

1. When a project is being developed on the dev branch, a bug appears in the project and needs to be repaired urgently. However, the content under development is only half completed and you do not want to submit it yet. At this time, you can use the git stash command to save the modified content to the stack. area, and then smoothly switch to other branches for bug repair. After the repair is completed, switch back to the dev branch again and restore the just saved content from the stack.
2 Due to negligence, the content that should have been developed on the dev branch was developed on the master. It needs to be switched back to the dev branch for development. You can use git stash to save the content to the stack, and after switching back to the dev branch, Just restore the content again.
3. If you want to pull the latest code, but you don’t want to re-add the commit record at this time, first git stash, then pull, and finally git stash pop. In general, the
function of the git stash command is to add the commit record that you don’t want to commit yet but have already The modified content is saved to the stack, and the content in the stack can be restored later on a certain branch . This means that the content in stash can not only be restored to the originally developed branch, but also to any other specified branch, and can span branches . The scope of git stash includes the content in the workspace and staging area, which means that all uncommitted content will be saved to the stack.

Detailed command explanation
1 git stash [Important]
It can save all uncommitted modifications (work area and staging area) to the stack for subsequent restoration of the current working directory.

$ 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 The function of git stash save
is equivalent to git stash. The difference is that you can add some comments, as follows:
The effect of git stash save "test1":

stash@{0}: On master: test1

3 git stash list
to view the contents of the current stash

4 git stash pop [Important]
Pop the contents of the current stash and apply it to the working directory corresponding to the current branch .
Note: This command deletes the recently saved content in the stack (the stack is first in, last out) and
executes the git stash save "test1" and git stash save "test2" commands sequentially. The effect is as follows:

$ 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

It can be seen that the stash of test2 pops out first. If there is no step-by-step stash, all files are in and out at the same time.
If the content recovered from stash conflicts with the content in the current directory, that is, the recovered content and the current directory modify the data in the same row, an error will be prompted and the conflict needs to be resolved. You can do this by creating a new branch. resolve conflicts

5 git stash apply
applies the contents of the stack to the current directory. Unlike git stash pop, this command will not delete the contents from the stack. In other words, this command can apply the contents of the stack to the working directory multiple times, adapting In the case of multiple branches

$ 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 contents of the stack are not deleted.
You can use git stash apply + stash name (such as stash@{1}) to specify which stash to restore to the current working directory
6 git stash drop + name
to remove a specified stash from the stack

7 git stash clear
clears everything in the stack

8 git stash show
to view the difference between the latest saved stash in the stack and the current directory.

9 git stash branch
creates a branch from the latest stash.
Application scenario: When part of the work is stored, ignore it for the time being and continue development on the current branch. Later, when you want to restore the content in stash to the current working directory, if it is a modification to the same file (even if it is not peer data), Then conflicts may occur and recovery fails, which is solved by creating a new branch. Can be used to resolve conflicts between the content in stash and the content in the current directory.
When conflicts occur, they need to be resolved manually.

Others:
By default, git stash caches the following files:

  • Modifications added to the staging area (staged changes)
  • Modifications tracked by Git but not added to the staging area (unstaged changes)

But the file will not be cached:

  • New files in the working directory (untracked files)
  • Ignored files (ignored files)
    are files that are not in git version control and cannot be saved by git stash. Therefore, newly added files will not be stored if stash is executed directly . At this time, you need to execute git add first to add it to git version control, and then git stash.

Reference links:
1. https://blog.csdn.net/stone_yw/article/details/80795669

Guess you like

Origin blog.csdn.net/qq_44804542/article/details/115875034