7. Git stash command

[TOC]

Foreword

This article only records some of your own opinions, which may not be accurate. It is just for some records during learning.

This article mainly introduces the use of the command git stash. What scenarios does the command apply to? As described below

In our enterprise development, there are multiple branches, such as a master in an online environment, and a dev branch for research and development. Suppose we have a requirement and are writing code in the dev branch. When we write half There is a serious bug in the online environment that must be resolved immediately. At this time, we cannot immediately go back to the master branch for modification, otherwise, half of the code written will be lost. At this point, you can use the git stash command to help.

The git stash command can save all the changes in the current workspace, restore to the latest code of the current branch, and then use the command to restore all the previously saved changes when needed.

A clearer explanation with the help of a blog is: https://blog.csdn.net/stone_yw/article/details/80795669

In general, the purpose of the git stash command is to save the content that has not been submitted yet but has been modified to the stack, and the content in the stack can be restored on a branch later

Next, let's demonstrate how to save the modified code.

1. git stash

[root@huangzb mygit]# git branch
* master
  new_branch
[root@huangzb mygit]#
[root@huangzb mygit]#
[root@huangzb mygit]# git checkout new_branch
Switched to branch 'new_branch'
[root@huangzb mygit]# ll
total 8
-rw-r--r-- 1 root root 24 Mar 27 18:15 a.txt
-rw-r--r-- 1 root root 12 Mar 27 18:15 b.txt
[root@huangzb mygit]#
[root@huangzb mygit]# echo 'hello b' > a.txt
[root@huangzb mygit]#
[root@huangzb mygit]# git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        a.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
[root@huangzb mygit]#
 

In the above figure, the existing file is modified in the new branch, and then an exception occurs when switching branches, that is, there is a modified file, either save or submit. Let ’s demonstrate how to save

Some people may ask that the two are in different branches, why not switch the branch after submitting the code directly?

In fact, you can submit the code before switching branches, but here is to simulate our premise, assuming that the code has not been written and cannot be submitted

Let's use the git stash command to save the operation, as shown below:

[root@huangzb mygit]# git stash
Saved working directory and index state WIP on new_branch: 0dae8a5 create b.txt
HEAD is now at 0dae8a5 create b.txt
[root@huangzb mygit]#
[root@huangzb mygit]# git status
# On branch new_branch
nothing to commit, working directory clean
[root@huangzb mygit]# git checkout master
Switched to branch 'master'
[root@huangzb mygit]#
 

As you can see from the picture above, after using the git stash command, the file modification will be saved first. In fact, simply use git stash to save the operation, use the default rules to generate comments, of course, we can also specify the comment message ourselves,

2. git stash save [message]

Use the git stash save command to specify notes to save

The operation is as follows:

[root@huangzb mygit]# echo 'hello c' > a.txt
[root@huangzb mygit]#
[root@huangzb mygit]# git stash save '将a.txt 内容覆盖为 hello c'
Saved working directory and index state On new_branch: 将a.txt 内容覆盖为 hello c
HEAD is now at 0dae8a5 create b.txt
[root@huangzb mygit]#
 

So, after we save, how to see the record, we can use the following command git stash list

3. git stash list

Use the command git stash list to view

When we use the git stash command to save the specified operation, a record will be formed, then if we want to view the list of records in the current branch, we can use the command git stash list to view, the operation is as follows:

[root@huangzb mygit]# git stash list
stash@{0}: On new_branch: 将a.txt 内容覆盖为 hello c
stash@{1}: WIP on new_branch: 0dae8a5 create b.txt
[root@huangzb mygit]#

As can be seen from the above figure, there are two records saved, and different message notes are displayed, and you can see that the way to save this record uses the stack data structure in git, which is the first-in-first-out method. The top record is the most recent commit.

So although we can see the submitted list, how do we see the saved content?

You can use the git stash show command to see

4. git stash show

You can use the git stash show command to view the differences of the most recently saved files

The operation is as follows:

[root@huangzb mygit]# git stash show
a.txt | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

From the above picture, you can only see which file has been modified, and how many lines have been deleted, how many lines have been added, and nothing can be seen. At this time, you can add the parameter -p to see more clear, try

[root@huangzb mygit]# git stash show -p
diff --git a/a.txt b/a.txt
index 40f4dc0..947f8df 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1 @@
-hello 1
-hello 2
-hello 3
+hello c
 

After adding the -p parameter, you can see very clearly.

Before we saved the operation, we needed to switch branches to do other things. Now that we are done, we need to restore the original data and continue to work. What should we do at this time? That is, how to restore the saved data

You can use the command git stash pop

5. git stash pop

Restore the most recent one of the saved records to restore, similar to the stack operation of the stack in java, get the latest data, and delete the record

Let's take a look at the effect, the operation is as follows:

[root@huangzb mygit]# git stash list
stash@{0}: On new_branch: 将a.txt 内容覆盖为 hello c
stash@{1}: WIP on new_branch: 0dae8a5 create b.txt
[root@huangzb mygit]# cat a.txt
hello 1
hello 2
hello 3
[root@huangzb mygit]#
[root@huangzb mygit]# git stash pop
# On branch new_branch
# 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:   a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (1c809fc6d4936282af1e4177c3e99dc456b3da75)
[root@huangzb mygit]# cat a.txt
hello c
[root@huangzb mygit]# git stash list
stash@{0}: WIP on new_branch: 0dae8a5 create b.txt
[root@huangzb mygit]#
 

It can be seen from the above figure that there were originally two records in the list. After using the pop command, it was found that the content of the file a.txt was restored to the state before saving. After using the command git stash list again, there was only one record, and the Is the top record before, so summarize the characteristics of the git stash pop command

  1. Restore recent data
  2. Delete Record

Then the pop command is to restore the most recent operation. If you want to restore the record at a certain moment in the list list, you can use the command git stash apply.

6. git stash apply [stash name]

Use the command apply to restore the record with the specified name. If you do not write the name, the latest item in the list is restored by default. However, this command only restores the data, and the record is unclear.

The operation is as follows:

[root@huangzb mygit]# git stash list
stash@{0}: WIP on new_branch: 0dae8a5 create b.txt
[root@huangzb mygit]#
[root@huangzb mygit]#
[root@huangzb mygit]# git stash apply stash@{0}
error: Your local changes to the following files would be overwritten by merge:
        a.txt
Please, commit your changes or stash them before you can merge.
Aborting
[root@huangzb mygit]#
 

It can be seen from the above figure that the abnormality is found after using the command, because we used the command to restore the most recent operation. At this time, the work space is not clean, so you cannot restore another saved record. Therefore, you need to first restore The current operation is saved or discarded. Here we first save and come back to restore the history. The operation is as follows:

[root@huangzb mygit]# git commit -am 'stash first'
[new_branch a8d8b2a] stash first
1 file changed, 1 insertion(+), 3 deletions(-)
[root@huangzb mygit]#
[root@huangzb mygit]# git status
# On branch new_branch
nothing to commit, working directory clean
[root@huangzb mygit]#
[root@huangzb mygit]# git stash apply stash@{0}
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
[root@huangzb mygit]# cat a.txt
<<<<<<< Updated upstream
hello c
=======
hello b
>>>>>>> Stashed changes
[root@huangzb mygit]#
 

It can be seen that after we save the record, we restore the original operation. After the restoration, we found that the file conflicted. The conflict can be resolved below.

[root@huangzb mygit]# echo 'hello aa' > a.txt
[root@huangzb mygit]# git commit -am '解决冲突'
[new_branch c26203d] 解决冲突
1 file changed, 1 insertion(+), 1 deletion(-)
[root@huangzb mygit]# git status
# On branch new_branch
nothing to commit, working directory clean
[root@huangzb mygit]#
 

Here I will simply and violently resolve the conflict. The point is that let's see if there are any records in the list

[root@huangzb mygit]# git stash list
stash@{0}: WIP on new_branch: 0dae8a5 create b.txt
[root@huangzb mygit]#

We found that even after using the apply command to restore the operation, there are still records, so we can summarize the differences between the apply and pop operations:

  1. The pop command can only restore the most recent record, cannot specify the record name to restore, and apply can specify the record name to restore
  2. After the pop command restore operation, the records will be deleted at the same time, and apply only restores the data, and the records will not be deleted

At this point, the records in the list are no longer necessary for us. What if we want to delete them?

You can use the command git stash drop [stash name] to delete records

7. git stash drop [stash name]

Use the drop command to delete the record with the specified message name

The operation is as follows:

[root@huangzb mygit]# git stash drop stash@{0}
Dropped stash@{0} (f56c4829229393701a666f0d198f467f592e54a1)
[root@huangzb mygit]#
[root@huangzb mygit]# git stash list
[root@huangzb mygit]#
 

Of course, if there are many records, deleting them one by one is more troublesome, you can delete all the records at once

8. git stash clear

In the case of many records, deleting one by one is more troublesome, you can delete all records at once

Since there is no data in the current list, it will not be demonstrated, it is relatively simple

Guess you like

Origin www.cnblogs.com/duguxiaobiao/p/12691409.html