1. Test scenario 1:
Assume that index.html is already in the code repository. Now you modify the index.html page, then add to temporarily store the index.html page, then continue to modify the index.html page, and then git stash, this time the code warehouse is clean, the modifications are temporarily stored, and then execute git stash pop to restore the modifications. At this time, it is found that the modifications in the workspace are restored, but the modifications in the staging area are lost. That is, git status found that no modifications were staged, and the previously staged modifications were discarded, but the modifications in the workspace were completely retained.
2. Test scenario two:
Now add the test01.html page, git status shows that test01.html is not tracked by git, and then modify the index.html file. At this time, git stash finds that the index.html file is temporarily Save, but the test01.html file is not temporarily saved. That is, git status does not temporarily store files that are not tracked by git.
3. Test scenario three:
Continue scenario two, modify the index.html file, and add the test01.html file as a new file. At this time, git add the test01.html file, let test01.html be tracked by git, and then git stash, you can see that the test01.html file is temporarily stored through the git stash show method, then git stash pop, the modifications are restored, git status found that the test01.html file is in a temporary state, and index.html is still a retained workspace modification. That is, git add a file that has not been tracked by git before. After git stash, this file will be temporarily stored, and after git stash pop, the newly added file will still be in a temporary state. Comparative test scenario 1, index In the .html file, only the modifications in the workspace are temporarily stored, and the modifications in the temporary storage area are lost.
4. Test scenario four:
Continue scenario three, modify the index.html file, and add the test01.html file as a new file. At this time, git add the test01.html file, let test01.html be tracked by git, and then Modify the test01.html file, then git stash, git stash pop, and find that test01.html is still in a temporary state, but the content of the temporarily saved file is the final modified content of the workspace. That is, git add a file that has not been tracked by git before, and then modify the file. After git stash, the file will be temporarily stored, and after git stash pop, the newly added file will still be in a temporary state, but The temporarily saved modification content is the modification content of the previous workspace rather than the temporary modification content through add.
Through the above test, I did not find the working pattern of git stash, and I was very confused. I hope a git expert can give me guidance and analysis.
First, let me explain the basic function of the
git stash
command: handle the dirty status of the working directory, that is, modified tracking files and temporary changes , and then save the unfinished modifications to a stack . After knowing this, let’s take a look at a few key points I summarized:The
git stash
command will push tracked files onto the stack, while untracked files will not be pushed onto the stack, as described in the poster's experiment 2.If the previous work has been added to the temporary storage area , the
git stash
command will not add the modification that pops the stack to the temporary storage area by default, just as described in the poster's experiment 1, but the poster's description is actually wrong , the previously temporarily saved modifications are not abandoned or lost but remain in the work area, but are not added to the temporary storage area.Regarding the newly added file test01.html in the original poster’s experiments 2 and 3, this is actually a special situation. We can understand it this way: The result of
stash pop
is to restore the previous modifications, and the recovery operation it can do for test01.html can only be to rejoin the temporary storage area. Otherwise, test01.html will return to an untracked state. .If you want to re-apply the modifications that were previously temporarily saved, you can use the
git stash pop
后加上--index
option. In this way, you can restore it to exactly the same as before, that is, what was temporarily saved before will now be in a temporary state and remain temporary. It is in an unstaged state.Finally, I need to state what the poster said:
or
In fact, the description is wrong, or it should not be described like this, because it will cause us to misunderstand - we may mistakenly think that
The poster can redo the above experiment to verify my statement. 🎜,而实际情况应该是这样的:git stash
只会暂存工作区的修改git stash
will only temporarily store the modifications of the workspace, but the actual situation should be like this: Default The restored operations will be retained in the workspace, but will not automatically re-stage them for you.