git stash 用法总结分析

git stash 用法总结分析

在git使用中,我们可能会面临以下几种情况。

  1. pull代码之前,需要把已修改文件暂存。
  2. pull代码的时候遇到和工作区修改的文件冲突情况,为了避免拉取冲突,需要把已修改文件暂存,pull之后再解决冲突;
  3. 在功能开发过程中,遇到急需修复的bug,需要修改的文件可能和现在开发的相同,需要将已经开发的内容暂存,使正常开发和bug修复隔离,然后进行bug修复,修复完提交。

git stash的作用是把修改的文件暂存到本地,将工作区文件还原至上次git更新、提交时的状态。新增文件不能暂存,也不会被删除。

git stash

先来看下当前 git 工作区的状态

$ git status
On branch dev
Your branch is up to date with 'origin/dev'.

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:   app/controller/login.ts
 modified:   app/controller/user.ts
 modified:   app/middleware/jwtHandler.ts
 modified:   app/service/user.ts

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 gitTest.ts

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

可以发现本地有四个文件被修改过,gitTest.ts 文件没有在Git版本控制中,是新增的文件,执行 git stash

$ git stash
Saved working directory and index state WIP on dev: f4d0a85 chore: 注释测试代码

该命令把工作区修改的文件保存起来,并将工作区恢复至上次git操作的状态,再次使用 git status

$ git status
On branch dev
Your branch is up to date with 'origin/dev'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 gitTest.ts

nothing added to commit but untracked files present (use "git add" to track)

可以发现工作区里gitTest.ts文件是Untracked状态,说明 git stash 不能保存新增的文件,其余四个被修改过的文件被保存,工作区恢复上次git代码更改时的状态。

git stash save

git stash save 功能和git stash 是一样的,区别在于 git stash save 可以为存储添加备注信息。

$ git stash save '测试git stash!'
Saved working directory and index state On dev: 测试git stash!

git stash list

$ git stash list
stash@{0}: On dev: 测试git stash!
stash@{1}: WIP on dev: f4d0a85 chore: 注释测试代码

git stash list 用来查看当前拥有的存储的条目,条目会列出条目名称、git分支名称以及stash是的描述信息。

git stash pop

git stash pop 用于将存储条目列表的最新一条条目删除,并将改条目应用于当前工作区。

$ git stash pop
On branch dev
Your branch is up to date with 'origin/dev'.

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

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 gitTest.ts

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (7f5237ffc5b4fa1d70b23710dab26762f62f7bb6)

通过执行 git stash pop 可以看出,.gitignore 文件被恢复至工作区,stash@{0} 被删除了。

想要pop指定的条目是,只需要在命令后添加条目名称 git stash pop stash@{1}

git stash apply

git stash applygit stash pop 类似,但是apply不会删除条目列表中的项。

$ git stash apply stash@{0}
On branch dev
Your branch is up to date with 'origin/dev'.

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

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 gitTest.ts

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

$ git stash list
stash@{0}: On dev: 测试git stash!
stash@{1}: WIP on dev: f4d0a85 chore: 注释测试代码

git stash show

$ git stash show
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

git stash show 用于展示最新一条存储条目的内容和最新一次提交时的更改情况。上面的输出结果表明.gitignore 文件被修改,插入了一行。

git stash show -p

$ git stash show -p stash@{1}
diff --git a/app/controller/login.ts b/app/controller/login.ts
index 40fc025..218c2f0 100644
--- a/app/controller/login.ts
+++ b/app/controller/login.ts
@@ -7,15 +7,16 @@ export default class HomeController extends BaseController {
     console.log(userName, password);
     const res = await ctx.service.login.account({ userName, password });
     if (res) {
-      const { user_code, user_role } = res;
+      const { user_code, user_role, user_id } = res;
       const resParams = {
         userName,
         userRole: user_role,
         userCode: user_code,
+        userId: user_id,
       };
       const token = app.jwt.sign({
         ...resParams,
-      }, app.config.jwt.secret);
+      }, app.config.jwt.secret, { expiresIn: '1d' });
       console.log(token, res);
       ctx.set({ Authorization: token });// 设置headers
       // const res = await ctx.service.user.getUser();
:

git stash show -pgit stash show 作用类似,在拥有多个存储条目的时候,git stash show -p 加上条目名称(stash@{1})可以具体查看某个条目的变更信息。

git stash branch

git stash branch 从最新提交的条目,创建并检出一个名为 < branchname > 的新分支,将 < stash > 中记录的更改应用于新的工作树和索引。如果成功,并且 < stash > 是 stash@{ < revision > }形式的引用,那么它将删除 < stash > 。

$ git stash branch 'stash-branch'
Switched to a new branch 'stash-branch'
On branch stash-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:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 gitTest.ts

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (b63ef4114670d27c8b0f9d61981bb85046924369)

$ git branch
  dev
  master
* stash-branch

$ git stash list
stash@{0}: WIP on dev: f4d0a85 chore: 注释测试代码

git stash drop

git stash drop 用于从存储条目列表删除某个条目。

$ git stash list
stash@{0}: On dev: 测试git stash pop!
stash@{1}: WIP on dev: f4d0a85 chore: 注释测试代码

$ git stash drop stash@{0}
Dropped stash@{0} (d4d647002d7d6887c14848c9f535fd7df75ad906)

$ git stash list
stash@{0}: WIP on dev: f4d0a85 chore: 注释测试代码

git stash clear

git stash clear 用于清空存储条目列表中的所有条目。

$ git stash list
stash@{0}: On dev: 测试git stash clear!
stash@{1}: WIP on dev: f4d0a85 chore: 注释测试代码

$ git stash clear

$ git stash list

Author: Liquorxm
Created: Jul 31, 2020 10:34 AM
Tags: git, git stash

©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页