Summary of git stash usage

1 Introduction

scene 1:

If is developing on branch A, but the code of branch B detects a bug and needs to be modified, so it is necessary to switch from branch A to branch B .

This can be done like this: after add + commit on branch A, switch to branch B. But there are 2 problems with this:

  1. Added unnecessary commits, although they can be modified later through git commit --amend,
  2. Half of the functions of branch A have been written. If you commit at this time, when you switch back to branch A, you have to check the commit to know the previously modified content, which is quite troublesome.

Scenario 2:

Halfway through the development of branch A, I discovered that the wrong branch was used. It should be developed on branch B. All currently modified content needs to be moved to branch B without affecting the current branch and version library.


In the above situation, the best way is to use stashes storage stack. All its operations will not affect the version library.

2. Commonly used commands

stashes storage stack, followlast-in-first-out.

2.1, basics

# 查看帮助(所有命令列表)
git stash -h
# 将当前工作区和暂存区的代码存储到 stashes栈中。
git stash
# 取出最近的一条 stash,并在 stashes 栈中删除。
git stash pop
# 取出最近的一条 stash,stashes 栈中不删除。
git stash apply

# 不取出,直接在 stashes 栈中删除最近的一条 stash
git stash drop

Because,git stash pop = git stash apply + git stash drop

# 查看 stashes 列表
git stash list
# 清空 stashes 栈
git stash clear

so,

Scenario 1 can be executed directly on branch Agit stash. After branch B is developed, switch to branch A and execute git stash pop is enough.

Scenario 2 can be executed directly on branch Agit stash, and then switched to branch B and executedgit stash pop .

2.2, advanced

1. Specify remarks when saving

git stash save 测试stash
# or
git stash push -m 测试stash

Missing picture to view the effect

2. Operate the specified storage through the index

# 操作 stash@{1}
git stash pop 1
# or 这里一定要加引号,否则报错。
git stash pop "stash@{1}"

git stash apply Sum git stash drop Same theory.

3. Modify storage rules

Default storage rules:Excludesnew files in the workspace (untracked files) and.gitignore Ignored files, including modifications to the workspace and staging area.

  1. -u or --include-untracked means to include untracked files.
git stash save 备注 -u
  1. -a or --all means including files ignored by .gitignore.
git stash save 备注 -a
  1. -k or --keep-index indicates that modifications to the staging area are not included.
git stash save 备注 -k

2.3, check the specific content of stash modification

View commandgit stash show is not introduced, because the version management that comes with vscode is very easy to use. You can view in detail what modifications have been made to each stash.

You can see not only the stashes, but also each commit can be viewed at any time.

Insert image description here


that's all.

Guess you like

Origin blog.csdn.net/qq_40147756/article/details/134546657