IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> git stash -> 正文阅读

[开发工具]git stash

git stash -help我们可以看到stash相关的命令,下面我们分别介绍一下它们

usage: git stash list [<options>]
   or: git stash show [<options>] [<stash>]
   or: git stash drop [-q|--quiet] [<stash>]
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash clear
   or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
          [-u|--include-untracked] [-a|--all] [-m|--message <message>]
          [--pathspec-from-file=<file> [--pathspec-file-nul]]
          [--] [<pathspec>...]]
   or: git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
          [-u|--include-untracked] [-a|--all] [<message>]
1、git stash
?  [/Users/test/Desktop/ruby] git:(master) vi feature_test_file1
?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   feature_test_file1

no changes added to commit (use "git add" and/or "git commit -a")
进行多次修改并satsh
?  [/Users/test/Desktop/ruby] git:(master) ? git stash save "第一次修改stash"
Saved working directory and index state On master: 第一次修改stash
?  [/Users/test/Desktop/ruby] git:(master) vi feature_test_file1
?  [/Users/test/Desktop/ruby] git:(master) ? git stash save "第二次修改stash"
Saved working directory and index state On master: 第二次修改stash
?  [/Users/test/Desktop/ruby] git:(master) vi feature_test_file1
?  [/Users/test/Desktop/ruby] git:(master) ? git stash save "第三次修改stash"
Saved working directory and index state On master: 第三次修改stash

在每一次stash的时候,我们使用git stash save "message",之后我们利用git stash list 查看目前暂存区的状态。

?  [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: 第三次修改stash
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash

(2)新建文件之后使用stash时,不会stash文件

原因是新建的文件没有在git版本控制,所以不能stash,可以使用两种方式stash

  • 需要先执行git add该文件将其加入到版本控制中,在git stash
?  [/Users/test/Desktop/ruby] git:(master) touch feature_test_file4
?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file4

nothing added to commit but untracked files present (use "git add" to track)
?  [/Users/test/Desktop/ruby] git:(master) ? git stash
No local changes to save
?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file4

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

?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file4
# 需要先执行git add该文件将其加入到版本控制中,在git stash
?  [/Users/test/Desktop/ruby] git:(master) ? git add .
?  [/Users/test/Desktop/ruby] git:(master) ? git stash
Saved working directory and index state WIP on master: 5598714 init
?  [/Users/test/Desktop/ruby] git:(master) git st
On branch master
nothing to commit, working tree clean

(3) git stash save -u “message”

?  [/Users/test/Desktop/ruby] git:(master) git stash apply stash@{0}
Already up to date!
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file5

nothing added to commit but untracked files present (use "git add" to track)
?  [/Users/test/Desktop/ruby] git:(master) ? vi feature_test_file5
?  [/Users/test/Desktop/ruby] git:(master) ? git stash save -u "jj"
Saved working directory and index state On master: jj
?  [/Users/test/Desktop/ruby] git:(master) git stash list
?  [/Users/test/Desktop/ruby] git:(master)
stash@{0}: On master: jj
stash@{1}: WIP on master: 5598714 init
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash


?  [/Users/test/Desktop/ruby] git:(master) vi feature_test_file5
?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file5

nothing added to commit but untracked files present (use "git add" to track)
?  [/Users/test/Desktop/ruby] git:(master) ? git stash save -u "hh"
stash@{0}: On master: hh
stash@{1}: On master: jj
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: WIP on master: 5598714 init
stash@{5}: On master: 第一次修改stash
  • 指定 --include-untracked-u 选项,Git 也会贮藏任何未跟踪文件。

在贮藏中包含未跟踪的文件仍然不会包含明确 忽略 的文件。 要额外包含忽略的文件,请使用 --all-a 选项。

?  [/Users/test/Desktop/ruby] git:(master) touch feature_test_file5
?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file5

nothing added to commit but untracked files present (use "git add" to track)
?  [/Users/test/Desktop/ruby] git:(master) ? git stash -u
Saved working directory and index state WIP on master: 5598714 init
?  [/Users/test/Desktop/ruby] git:(master) git st
On branch master
nothing to commit, working tree clean
?  [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第二次修改stash
stash@{5}: On master: 第一次修改stash

#但是修改过未追踪的文件之后,git st显示的信息还是untrack
?  [/Users/test/Desktop/ruby] git:(master) vi feature_test_file5
?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file5

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

2、查看stash内容

  • git stash list: 查看stash列表

  • git stash show: 查看第一个储藏做了哪些改动,不是改动详情

  • git stash show stash@{num}: 查看其他储藏做了哪些改动

  • git stash show -p: 查看第一个储藏的改动

  • git stash show -p stash@{num}: 查看其他储藏做的改动

?  [/Users/test/Desktop/ruby] git:(master) git stash show stash@{1}
feature_test_file1 | 1 +
 1 file changed, 1 insertion(+)
?  [/Users/test/Desktop/ruby] git:(master) git stash show -p stash@{1}
diff --git a/feature_test_file1 b/feature_test_file1
index e69de29..dd98f1d 100644
--- a/feature_test_file1
+++ b/feature_test_file1
@@ -0,0 +1 @@
+第二次修改

3、应用stash内容

  • git stash apply: 应用第一个储藏,但不会删除
  • git stash apply stash@{num}: 应用列表中的储藏
  • git stash pop: 应用第一个储藏并删除
  • git stash pop stash@{num}: 应用stash list中的第num个储藏并删除

(1)下面我们通过实例看一下git stash apply

?  [/Users/test/Desktop/ruby] git:(master) git stash apply stash@{1}
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   feature_test_file1

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

#可以看到git stash apply stash@{num}应用了stash,但是没有删除对应的stash
?  [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: 第三次修改stash
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash

(2)下面我们通过实例看一下git stash pop

?  [/Users/test/Desktop/ruby] git:(master) git stash pop stash@{0}
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   feature_test_file1

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

?  [/Users/test/Desktop/ruby] git:(master) ? vi feature_test_file1
第三次修改

?  [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash

(3)其他的小实验

  • 如果我们应用了一个stash之后未对修改保存,在应用其他stash会报错
?  [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   feature_test_file1

no changes added to commit (use "git add" and/or "git commit -a")
?  [/Users/test/Desktop/ruby] git:(master) ? vi feature_test_file1
?  [/Users/test/Desktop/ruby] git:(master) ? git stash apply stash@{0}
error: Your local changes to the following files would be overwritten by merge:
	feature_test_file1
Please commit your changes or stash them before you merge.
Aborting

最佳实践:应用一个stash之后确认是否使用,如果使用形成commit,如果不使用restore

4、删除stash内容

  • git stash drop: 删除第一个储藏

  • git stash drop stash@{num}: 删除stash list中的储藏,例如删除第二个:git stash drop stash@{1}

  • git stash clear: 删除所有储藏的stash

?  [/Users/test/Desktop/ruby] git:(master) git stash list
#这一条stash未使用stash save
stash@{0}: WIP on master: 5598714 init 
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash
?  [/Users/test/Desktop/ruby] git:(master) git stash drop
Dropped refs/stash@{0} (75aa0952a817c3d753674007983c1c792a8bd9c9)

?  [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: 第二次修改stash
stash@{1}: On master: 第一次修改stash

5、从stash创建分支

git stash branch 分支名字stash@{0}

?  [/Users/test/Desktop/ruby] git:(master) git stash branch feature_from_stash stash@{0}
Switched to a new branch 'feature_from_stash'
Already up to date!
On branch feature_from_stash
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_test_file5

nothing added to commit but untracked files present (use "git add" to track)
Dropped stash@{0} (f257c0cd558430e0550048149bbbb9a4be0eafaf)
?  [/Users/test/Desktop/ruby] git:(master) git log
commit 5598714bc98500e627b82bd1903d35c88d2b8e8f (HEAD -> feature_from_stash, master)
Author: test <test@staff.weibo.com>
Date:   Mon Sep 19 10:05:28 2022 +0800

    init

我们在master分支上最后的修改是在文件5里面输入了“第二次修改”并且保存了,我们看看文件5发现确实是该内容

第二次修改

我们看一下该分支有哪些stash

?  [/Users/test/Desktop/ruby] git:(feature_from_stash) ? git stash list
stash@{0}: On master: jj
stash@{1}: WIP on master: 5598714 init
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash

我们切到master分支看一下stash,发现两个分支的stash状态一样

?  [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: jj
stash@{1}: WIP on master: 5598714 init
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash

下面我们再看一个在git管理的文件的相关分支的操作

?  [/Users/test/Desktop/ruby] git:(feature_from_stash4) ? git stash list
stash@{0}: On master: master上修改file1
stash@{1}: On feature_from_stash3: stash3分支stash
stash@{2}: On feature_from_stash2: stash2分支stash
stash@{3}: WIP on master: 5598714 init
stash@{4}: WIP on master: 5598714 init
stash@{5}: On master: 第一次修改stash
?  [/Users/test/Desktop/ruby] git:(master) git stash branch feature_from_stash4  stash@{0}
Switched to a new branch 'feature_from_stash4'
On branch feature_from_stash4
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   feature_test_file1

no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (5bdf08d31455b2a3a6ede4647cec8a61d005f22f)
?  [/Users/test/Desktop/ruby] git:(feature_from_stash4) ? git stash list
stash@{0}: On feature_from_stash3: stash3分支stash
stash@{1}: On feature_from_stash2: stash2分支stash
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash
  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-09-24 21:15:37  更:2022-09-24 21:18:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/28 3:03:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计