git stash 缓存代码相关操作

1.基本用法

1.1保存当前代码

复制代码
git stash

作用:将当前工作目录中的所有未提交的更改(包括暂存区和工作区的更改)保存到一个"stash"中,并将工作目录恢复到最近一次提交的状态。

1.2 保存更改并添加描述

arduino 复制代码
git stash save "描述信息"

作用:保存当前更改,并为 stash 添加描述信息,方便后续查找。

示例

arduino 复制代码
git stash save "临时保存未完成的特性"

2. 查看所有保存的 Stash

复制代码
git stash list

作用:列出所有保存的 stash,显示每个 stash 的名称、描述和保存时的分支信息。

输出示例:

less 复制代码
stash@{0}: WIP on main: commit-hash 描述信息
stash@{1}: WIP on feature: commit-hash 另一个描述

3. 恢复 Stash

3.1 恢复最新的 Stash

perl 复制代码
git stash pop

**作用:**将最新的 stash 应用到当前工作目录,并从 stash 列表中删除该 stash。

**注意:**如果有冲突,Git 会提示冲突信息,需要手动解决。

3.2 恢复指定的 Stash

kotlin 复制代码
git stash apply stash@{索引}
// 可能会发生报错,需要加上'',后续需要指定stash的都是如此
git stash apply 'stash@{索引}'

**作用:**将指定的 stash 应用到当前工作目录,但不会从 stash 列表中删除。

示例:

kotlin 复制代码
git stash apply stash@{1}
// or
git stash apply 'stash@{1}'

3.3 恢复并删除指定的 Stash

kotlin 复制代码
git stash pop stash@{索引}

**作用:**将指定的 stash 应用到当前工作目录,并从 stash 列表中删除。

示例:

perl 复制代码
git stash pop stash@{1}
// or
git stash pop 'stash@{1}'

4. 删除 Stash

4.1 删除最新的 Stash

sql 复制代码
git stash drop

**作用:**删除最新的stash

4.2 删除指定的 Stash

kotlin 复制代码
git stash drop stash@{索引}

**作用:**删除指定的stash

用例:

kotlin 复制代码
git stash drop stash@{1}
// 
git stash drop 'stash@{1}'

4.3 清空所有 Stash

arduino 复制代码
git stash clear

**作用:**删除所有保存的stash

5. 查看 Stash 的内容

5.1 查看最新的 Stash

sql 复制代码
git stash show

**作用:**显示最新的 stash 中的更改。

5.2 查看指定的 Stash

kotlin 复制代码
git stash show stash@{索引}

作用:显示指定 stash 的更改。

5.3 查看 Stash 的详细内容

css 复制代码
git stash show stash@{索引} -p

**作用:**以补丁的形式显示指定 stash 的详细更改。

6. 常见场景

6.1 切换分支前保存更改

当你正在工作时,需要切换到另一个分支,但不想提交当前的更改:

复制代码
git stash
git checkout other-branch

6.2 恢复更改并继续工作

当你完成其他任务后,回到原来的分支并恢复更改:

perl 复制代码
git checkout original-branch
git stash pop

6.3 清理工作目录

如果你发现某个 stash 已经不再需要,可以直接删除:

kotlin 复制代码
git stash drop stash@{索引}
相关推荐
vibecoding日记1 天前
为什么我就想要「线性历史 + Signed Commits」,GitHub 却把我当猴耍 🤬🎙️
git·编程工具
程序员小崔日记1 天前
如何将代码轻松上传到 Gitee?Git 使用全攻略!
git·gitee·上传
Bigger2 天前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP3 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
红豆子不相思3 天前
Tomcat 环境搭建与集群实战
服务器·git·tomcat
杰哥技术分享3 天前
Git 仓库迁移技术文档:从 CODING.net 迁移至腾讯云 CNB
git
梅孔立3 天前
Ansible 100 台服务器一键管控实战 进阶版
服务器·git·ansible
qq_426003964 天前
git切换当前分支到远程分支
git
ON10N4 天前
100% 纯 Vibe Coding,我是怎么用 AI 撸出一个 VS Code 插件的
git·ai编程·visual studio code