git stash
时间: 2023-11-06 13:02:55 浏览: 197
git stash是一个用于临时保存工作进度的命令。当你需要切换到其他分支进行工作时,可以使用git stash命令将当前的修改保存到一个临时区域中,以便稍后再回到当前分支继续工作。git stash命令会将所有未提交的修改保存在一个新的stash中,并将工作目录恢复到上一个提交的状态。
git stash save命令是git stash的旧版本,用于保存工作进度并添加注释。这个命令将创建一个新的stash,你可以给它一个有意义的名称,以便更好地描述保存的修改内容。
git stash list命令可以列出所有保存的stash,方便查看和管理。
git stash show stash@{1} -p命令可以查看指定stash的差异内容,包括修改的文件和具体的修改内容。
git stash branch命令可以基于指定的stash创建一个新的分支,并切换到这个新的分支上。
相关问题
.git stash save "这里是注释"2.git pull3.git stash pop4.git stash list 5.git stash show
.git stash save "这里是注释":
这个命令用于将当前的工作目录中的更改保存到一个临时区域,以便稍后恢复。保存的更改可以附带一个注释,以便更好地描述保存的内容。
.git pull:
这个命令用于从远程仓库拉取最新的更改并合并到当前分支。它会自动下载远程仓库的最新更改,并尝试将其合并到当前分支。
.git stash pop:
这个命令用于从临时区域恢复最近保存的更改,并将其应用到当前分支。它会将最近保存的更改从临时区域中取出,并将其应用到当前分支上。
.git stash list:
这个命令用于列出当前存储在临时区域中的所有保存的更改。它会显示每个保存的更改的唯一标识符和注释。
.git stash show:
这个命令用于显示最近保存的更改的详细信息。它会显示最近保存的更改的文件列表以及每个文件的更改内容的摘要。
Git stash
### Git Stash 的用法与示例
Git stash 是一种用于临时保存当前工作目录和索引状态的工具,允许用户在切换分支或处理紧急任务时保留未提交的更改。以下是对 Git stash 的详细说明及使用示例:
#### 1. 基本命令
- **保存当前更改**
使用 `git stash` 或 `git stash save "message"` 将当前的工作目录和索引状态保存到 stash 列表中,并清除工作区的更改[^2]。
- **查看 stash 列表**
使用 `git stash list` 查看所有已保存的 stash 记录。每个记录都有一个唯一的标识符(如 `stash@{0}`)[^4]。
- **应用最近一次 stash**
使用 `git stash apply` 将最近一次 stash 的更改重新应用到工作区,但不会从 stash 列表中移除该 stash[^2]。
- **弹出最近一次 stash**
使用 `git stash pop` 将最近一次 stash 的更改重新应用到工作区,并从 stash 列表中移除该 stash[^2]。
- **删除所有 stash**
使用 `git stash clear` 删除所有的 stash 记录[^1]。
#### 2. 解决冲突场景
当分支发生较大变更时,直接应用 stash 可能会导致冲突。此时可以使用以下方法:
- 使用 `git stash branch <branch-name>` 创建一个新分支,并将 stash 的更改应用到该分支上。
#### 3. 示例代码
以下是一个完整的使用示例:
```bash
# 开始开发
$ git status
On branch main
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: file1.txt
# 保存当前更改到 stash
$ git stash
Saved working directory and index state WIP on main: abc1234 Initial commit
# 处理紧急任务
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
# 提交紧急修复
$ git add .
$ git commit -m "Fix critical bug"
# 返回原分支并恢复 stash
$ git checkout main
Switched to branch 'main'
$ git stash pop
Reapplying saved working directory and index state WIP on main: abc1234 Initial commit
```
#### 4. 典型问题解决
- 如果尝试切换分支时遇到本地更改会被覆盖的错误,可以使用 `git stash` 暂存更改后再切换分支[^3]。
- 如果 stash 应用失败,可以尝试使用 `git stash branch` 创建新分支以避免冲突[^1]。
### 注意事项
- stash 不会保存未跟踪的文件。如果需要保存这些文件,可以在命令中添加 `--include-untracked` 参数[^4]。
- stash 的内容是全局的,适用于所有分支。因此,在多个分支间切换时需小心管理 stash 内容。
阅读全文
相关推荐














