Use of Git Stash command

1. Usage scenarios

After pulling the code from the remote warehouse, some configuration files in the warehouse need to be modified to run the project normally. However, the changed file will affect the normal pull operation of Git (Git will prompt to process local changes before pulling).
At this time, it is more convenient to save the local modification with the stash command. Afterwards, the workspace becomes a "clean" state, ready for normal pull operations.
After the pull is complete, you can use the stash command to "take out" the previous stash content to the workspace, so that you can re-run the project.
Because of what the stash holds, it is possible to "take out" across branches. In the above scenario, the function is relatively powerful.

2. git stashIntroduction

Run git help stashthe command, you can see the help of this command:

NAME
       git-stash - Stash the changes in a dirty working directory away

DESCRIPTION
       Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
       The modifications stashed away by this command can be listed with git stash list, inspected with git stash show, and restored (potentially on top of a different commit) with git stash apply. Calling git stash without any arguments is equivalent to git stash push. A stash is by default listed as "WIP on branchname ...", but you can give a more descriptive message on the command line when you create one.
       The latest stash you created is stored in refs/stash; older stashes are found in the reflog of this reference and can be named using the usual reflog syntax (e.g. stash@{0} is the most recently created stash, stash@{1} is the one before it, stash@{2.hours.ago} is also possible). Stashes may also be referenced by specifying just the stash index (e.g. the integer n is equivalent to stash@{n}).

It means:

git-stash - 将一个修改后的工作区中的改动保存起来,将工作区恢复到改动前的状态。

具体描述:
当你想要保存工作区的当前状态,并想要回到一个干净的工作目录时,可以使用git stash命令。该命令保存本地修改,并将工作区恢复到HEAD指向的commit状态。
git stash保存的内容可以通过命令“git stash list”列出,可以通过“git stash show”命令查看,可以通过“git stash apply”命令恢复(可以恢复到不同的commit/分支上)。不加任何参数调用“git stash”命令等同于“git stash push”。Stash信息默认展示为"WIP on branchname ...",但是你可以在stash命令执行的时候,添加相关描述性的信息。
创建的最新的stash信息保存在"refs/stash",稍微早一点的stash可以通过这个引用的reflog查看,也可以通过通常的reflog语法命名规则指代。比如“stash@{0}”表示最新创建的stash,“stash@{1}”是更早些的stash。stash也可以只通过序号指代,比如"n"代表"stash@{n}"。

3. Examples

3.1 The following is an example of continuous stash saving and taking out

Check out the last commit:

$ git log -1
commit 1eff7133816e9e77d34c25dd63e017ab899bf490 (HEAD -> master, origin/master)
Author: XiaCheng <[email protected]>
Date:   Fri Mar 8 18:57:40 2019 +0800

    add doing mark

Change a file in a workspace:

$ echo "asdf" >> style.css 
$ git status
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

Save the changes to the workspace with the stash command:

$ git stash 
Saved working directory and index state WIP on master: 1eff713 add doing mark
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Modify a file in the workspace again:

$ echo "qwer" >> style.css
$ git status
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

Save with the stash command and add description information:

$ git stash save "add qwer to style.css"
Saved working directory and index state On master: add qwer to style.css

View stash list:

$ git stash list
stash@{0}: On master: add qwer to style.css
stash@{1}: WIP on master: 1eff713 add doing mark

Restore the saved content of the last stash:

$ git stash apply stash@{0}
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")
$ git status
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

3.2 git stash across branches

Since the contents saved by git stash have not been submitted yet, these contents are not branch-based (it has nothing to do with specific branches), but workspace-based. Below is an example.
Following the previous example, we first reset the previous modification, then create a new branch testBra, and modify style.css:

$ git checkout -- style.css 
$ git checkout -b testBra
Switched to a new branch 'testBra'
localhost:todo_man chengxia$ echo "test branch" >> style.css 
localhost:todo_man chengxia$ git status
On branch testBra
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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -am "add test content on testBra"
[testBra d9b757d] add test content on testBra
 1 file changed, 1 insertion(+), 1 deletion(-)

Next, we take out the content saved by the stash command before:

$ git stash list
stash@{0}: On master: add qwer to style.css
stash@{1}: WIP on master: 1eff713 add doing mark
$ git stash apply stash@{0}
Auto-merging style.css
CONFLICT (content): Merge conflict in style.css
$ git status
On branch testBra
Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

    both modified:   style.css

no changes added to commit (use "git add" and/or "git commit -a")
$ vim style.css

At this time, the prompt conflict is as follows:

Stash Conflict Content

3.2.1 git addIdentification conflict resolution

At this time, we can use the usual git addcommand to identify that the conflict has been resolved, as follows:

$ git add style.css 
$ git status
On branch testBra
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   style.css

$ 

However, sometimes this is not what we want, because we may not want to submit the modification of this file in the future. At this time, we can use the command to git resetidentify the conflict resolution.

3.2.2 git resetIdentification conflict resolution

First, we need to reset the workspace.
unstage:

$ git reset HEAD style.css 
Unstaged changes after reset:
M   style.css
$ git status
On branch testBra
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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

Reset modification:

$ git checkout -- style.css 
localhost:todo_man chengxia$ git status
On branch testBra
nothing to commit, working tree clean
$ git status
On branch testBra
nothing to commit, working tree clean

Re will stash@{0}restore:

Auto-merging style.css
CONFLICT (content): Merge conflict in style.css
$ git status
On branch testBra
Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

    both modified:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

The conflict is prompted again. This time, git resetthe conflict has been resolved by switching to the command flag.

$ git reset
Unstaged changes after reset:
M   style.css
$ git status
On branch testBra
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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

Note: When resolving file conflicts, the content of the file is not modified. Generally speaking, we need to modify the content of the file to what we want (remove the , and , etc. marks that identify the conflict <<<<<<) >>>>>>, ======and then mark that the conflict has been resolved.

4. git stashCommand reference

  • (1) git stash save "save message": When performing storage, add a note description.
  • (2) git stash list: View the stash list.
  • (3) git stash show: Show what specific changes have been made. By default, the first stash storage is displayed. If you want to display other storages, add them later stash@{$num}, such as the second one git stash show stash@{1}.
  • (4) git stash apply: Apply a certain storage, but will not delete the storage from the storage list. The first storage is used by default, that is, stash@{0}if you want to use other ones, add them git stash apply stash@{$num}, such as the second one git stash apply stash@{1}.
  • (5) 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, that is, stash@{0}. If you want to apply And delete other stash storage, command: git stash pop stash@{$num}.
  • (6) git stash drop stash@{$num}: Delete stash@{$num}storage.
  • (7) git stash clear: Delete all cached stash storage.

The newly added files will not be stored if the stash is executed directly. It needs to git addbe added to the git temporary storage area with the command before it can be git stashsaved.

Reprinted from: https://www.jianshu.com/p/e9764e61ef90

Guess you like

Origin blog.csdn.net/qq_27981847/article/details/130646425