Detailed explanation and use of Git stash series

Sometimes it is necessary to switch branches continuously in the project, so we don’t want to submit the modified code. What method can we use to successfully switch branches without submitting our own code?
The following is a series of usages of git stash. Stash means hiding in English. The function of git stash is also to hide unfinished code to prevent it from interfering with the work of others or new branches.

1、git stash

git stash caches the content that has not been submitted locally (the content of git commit will not be cached but the content of git add will be cached) and removes it from the current branch. The cached data structure is a stack, first in last out.

git stash can be used multiple times, each time a new stash@{num} is added, num is the number.

Among them, git stash is the same as git stash save, which caches and removes the content that has not been submitted, and the name of this cache is the content of the latest committed commit -m. If there is no local submission, it will pull the commit content of the remote warehouse .

git stash save "xxxxxxxxxxxx", plus your own cached annotations, easy to find (but in terms of usage, it is useless, because git stash will mark the content of the latest commit, using the Git Graph plug-in in vscode is more intuitive learn)

2、git stash list

The function of git stash list is to check which stores are stored in stash. The string before the colon of each line is to identify this hidden id

3、git stash pop

The command restores the previously cached working directory, deletes the corresponding stash in the cache stack, and applies the corresponding modification to the current working directory. The default is the first stash, namely stash@{0}. If you want to apply and delete other stash , command: git stash pop stash@{$num}, such as apply and delete the second: git stash pop stash@{1}

4、git stash apply

git stash apply is similar to git stash pop, but it will not delete the storage from the storage list (the pop command pops the cache into the application and deletes the cache of this application, while apply will not delete it, and it is still there when you switch back from the branch) , the first storage is used by default, that is, stash@{0}, if you want to use other storage, git stash apply stash@{$num}, such as the second one: git stash apply stash@{1}

5、git stash show

git stash show shows what changes have been made. By default, the first storage is shown. If you want to display other storages, add stash@{ KaTeX parse error: Expected 'EOF', got '}' at position 4: num}̲, for example The second git stas... num} -p, such as the second one: git stash show stash@{1} -p

6、git stash drop stash@{$num}

git stash drop stash@{$num} drops the stash@{$num} stash, removes this stash from the list. If you only have one cached, just use git stash drop directly. If there are more than one, you need to add the following quantity parameter.

At the same time, it should be noted that if you use git stash pop, it will delete the cache by itself, but if you use git stash apply, you need to manually delete the records in the list list (ie drop -p)

7、git stash clear

git stash clear is also a delete command, but it deletes all cached stash, so it should be used with caution!

8、git stash branch

git stash branch specifies or creates a branch in the latest cache, but I have never used this command!

9. It should be noted that! ! !

Newly added files will not be stored by directly executing stash, and files that are not in git version control cannot be stored by git stash

The newly added file after stash does not enter the cache. This is because git has not tracked the newly added file, so git add [file name] needs to be performed to let git track this file, and then stash can operate on the new file.

git add just adds the file to git version control, it does not mean it is stashed up, git add and git stash have no necessary relationship, but the premise that git stash can be stored correctly is that the file must be in git version control .

Usually, git stash apply is used more often, and others are used less. It is only written here for records. In addition, it is recommended that you use the Git Graph plug-in of vscode to see the cache list more intuitively! ! !

Reference:
https://www.cnblogs.com/zndxall/archive/2018/09/04/9586088.html

Guess you like

Origin blog.csdn.net/qq_44333320/article/details/128538380