git中stash的工作原理是什么?
淡淡烟草味
淡淡烟草味 2017-05-02 09:44:35
0
1
785

1.测试场景一:
假设index.html本来就在代码仓库中,现在修改了index.html页面,然后add暂存index.html页面,接着继续修改index.html页面,然后git stash,这个时候代码仓库干净了,修改被暂存,然后执行git stash pop,还原修改,这个时候发现,工作区的修改还原了,但是暂存区的修改丢失了。也就是git status发现,没有任何修改被暂存,之前暂存的修改被废弃了,但是工作区的修改完全被保留。

2.测试场景二:
现在添加test01.html页面,git status显示test01.html并没有被git跟踪,然后修改index.html文件,这个时候git stash,发现index.html文件被暂存,但是test01.html文件没有被暂存。也就是git status并没有暂存没有被git跟踪的文件。

3.测试场景三:
延续场景二,修改index.html文件,test01.html文件为新添文件,这个时候git add test01.html文件,让test01.html被git追踪,再git stash,通过git stash show方法可以看到test01.html文件被暂存,接着git stash pop,修改被还原,git status发现test01.html文件是被暂存状态,index.html还是被保留的工作区的修改。也就是git add 一个之前未被git追踪的文件,git stash后,这个文件会被暂存,而且git stash pop后,新添加的这个文件还是暂存状态,对比测试场景一中,index.html文件只是工作区的修改被暂存,暂存区的修改被丢失。

4.测试场景四:
延续场景三,修改index.html文件,test01.html文件为新添文件,这个时候git add test01.html文件,让test01.html被git追踪,然后再修改test01.html文件,接着git stash,git stash pop,发现test01.html依然是暂存状态,但是被暂存的文件内容却是工作区最终的修改内容。也就是git add 一个之前未被git追踪的文件,然后再修改这个文件,git stash后,这个文件会被暂存,而且git stash pop后,新添加的这个文件还是暂存状态,但是暂存的修改内容却是之前工作区的修改内容而不是通过add暂存的修改内容。


通过以上测试,我没有发现git stash的工作规律,非常晕,希望git高手给我指导分析一下。

淡淡烟草味
淡淡烟草味

reply all(1)
曾经蜡笔没有小新

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后加上--indexoption. 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:

The previously saved modifications have been discarded

or

The modifications in the temporary storage area were lost

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 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
.

The poster can redo the above experiment to verify my statement. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template