当前位置: 首页 > news >正文

Git Stash 使用与应用场景介绍(中英双语)

Git Stash 使用与应用场景介绍

在 Git 的日常开发中,我们常常会遇到一些突发的需求,或者需要在本地暂时中断当前的工作。此时,我们的工作区可能已经做了一些修改,但这些修改并未准备好提交或者需要在不同的分支之间切换。Git 提供了一个非常有用的命令——git stash,它允许我们将当前的修改暂存起来,从而恢复到一个干净的工作状态。

什么是 Git Stash?

git stash 是 Git 提供的一个功能,它允许你将当前工作区和暂存区的更改暂时“存储”起来,从而恢复到上一次提交的状态。你可以在稍后再恢复这些修改,继续你的工作。这个功能特别适用于需要在不同任务之间来回切换,但又不想丢失当前工作的修改时。

git stash 的基本用法

1. git stash —— 暂存修改

当你在当前分支上修改了一些文件,但不想提交这些更改,只是想切换到另一个分支时,可以使用:

git stash

执行后,Git 会将你的修改暂时存储起来,并恢复到当前分支的上一个提交的状态。此时,工作区和暂存区都将恢复到干净的状态。

2. git stash list —— 查看暂存的修改

你可以通过以下命令查看所有已暂存的修改列表:

git stash list

Git 会列出所有已经存储的暂存项,通常显示为:

stash@{0}: WIP on master: 5e78a1d Update readme
stash@{1}: WIP on develop: a2398f0 Add new feature

stash@{0} 表示最新的存储项,stash@{1} 表示倒数第二项,依此类推。

3. git stash pop —— 恢复暂存的修改并删除

如果你想恢复最近一次存储的修改,并且删除该存储项,可以使用:

git stash pop

恢复后,Git 会把修改应用到当前分支,并且把这次暂存的记录从 stash list 中移除。

4. git stash apply —— 恢复暂存的修改但不删除

如果你希望恢复修改,但不想删除该存储项,可以使用:

git stash apply

这样,修改会被恢复到工作区,但暂存项仍然保留在 stash list 中,可以稍后再次恢复。

5. git stash drop —— 删除暂存的修改

如果你不再需要某次存储的修改,可以删除它:

git stash drop stash@{0}

这会删除指定的暂存项。如果你想清空所有的暂存项,可以使用:

git stash clear

Git Stash 的常见应用场景

1. 切换分支时暂存当前修改

假设你正在开发某个功能,已经在代码中做了一些修改,但此时你突然需要切换到另一个分支进行紧急修复或功能开发。你不想提交当前的修改,也不希望丢失这些修改。此时,git stash 就能派上用场。

git stash          # 暂存当前修改
git checkout fix   # 切换到修复分支
# 完成修复后切换回原分支
git checkout feature
git stash pop      # 恢复之前的修改
2. 保存当前进度,稍后继续工作

在开发过程中,你可能会突然想尝试其他方法或进行不同的实验。这时,可以将当前的进度暂时保存下来,等到尝试完成后,再恢复进度继续原来的工作。

git stash           # 保存当前进度
# 尝试其他方法或开发新功能
git stash pop       # 恢复原来的进度
3. 临时撤销部分修改

有时候,你只需要撤销部分文件的修改,其他文件的修改则需要保留。你可以通过 git stash 暂时保存所有修改,然后逐个恢复特定的文件。

git stash              # 暂存所有修改
git stash apply stash@{0} -- open_instruct.py  # 只恢复某个文件
4. 解决合并冲突时使用

在合并操作(如 git mergegit pull)时,如果遇到冲突,Git 会让你手动解决冲突。在解决冲突之前,你可能需要保存当前的工作状态,以免丢失文件。此时,你可以使用 git stash 暂存修改,待合并解决冲突后再恢复原来的修改。

git stash          # 暂存工作进度
git pull           # 拉取远程更新并解决冲突
git stash pop      # 恢复工作进度

Git Stash 的高级用法

1. 暂存指定文件的修改

默认情况下,git stash 会保存所有修改的文件。如果你只想暂存某个文件的修改,可以使用:

git stash push -m "message" <file_path>

例如:

git stash push -m "暂存了 finetune_peft.py 文件的修改" open_instruct/finetune_peft.py
2. 暂存与恢复特定的修改部分

git stash 也可以存储并恢复特定的暂存区和工作区的变化。例如:

git stash save --keep-index  # 只保存未暂存的修改

这个命令会把所有暂存区外的修改存储起来,保持暂存区的内容不变。

小结

git stash 是 Git 提供的一个非常实用的工具,可以让我们在开发过程中方便地存储当前的工作进度,从而切换分支或处理紧急任务时不丢失修改。通过 git stash,你可以在不同的工作之间切换自如,同时保持开发的连续性。

应用场景总结

  1. 临时保存当前工作进度,切换分支。
  2. 解决合并冲突时避免丢失修改。
  3. 暂时撤销部分修改,稍后再恢复。

掌握 git stash 可以大大提高你的开发效率,避免丢失重要修改,并使你在多个任务间轻松切换。

Introduction to Git Stash: Usage and Application Scenarios

In the daily use of Git, there are times when we encounter sudden needs or have to temporarily interrupt our current work. During such times, we may have modified some files locally, but these changes are not yet ready to be committed, or we need to switch between different branches. Git provides a very useful command—git stash—which allows us to temporarily “store” our current changes, so we can revert to a clean working state and continue our work later.

What is Git Stash?

git stash is a Git feature that allows you to temporarily “store” changes from your working directory and staging area. This enables you to return to the state of the last commit without losing your local changes. Later, you can apply the stashed changes and continue working on them. This command is especially useful when you need to switch between tasks but don’t want to lose your progress.

Basic Usage of git stash

1. git stash — Stash Changes

When you’ve made changes to files but don’t want to commit them yet, and you need to switch branches, you can use:

git stash

After executing this command, Git will temporarily store your modifications and revert the working directory and staging area to the state of the last commit. Your changes are saved, and you can switch to another branch without worrying about losing them.

2. git stash list — View Stashed Changes

To see a list of all stashed changes, use:

git stash list

Git will list all the stashes you have saved, typically showing something like this:

stash@{0}: WIP on master: 5e78a1d Update readme
stash@{1}: WIP on develop: a2398f0 Add new feature

Here, stash@{0} refers to the most recent stash, and stash@{1} refers to the second one, and so on.

3. git stash pop — Apply and Remove the Latest Stash

If you want to restore the most recent stash and remove it from the list, you can use:

git stash pop

After this, your changes will be restored to the working directory, and the stash will be removed from the stash list.

4. git stash apply — Apply the Stash Without Removing It

If you want to restore the stash without removing it from the list, use:

git stash apply

This command applies the stashed changes to the working directory but leaves the stash in the list, so you can apply it again later if needed.

5. git stash drop — Remove a Stash

If you no longer need a particular stash, you can delete it:

git stash drop stash@{0}

To remove all stashes, use:

git stash clear

Common Application Scenarios for git stash

1. Stashing Changes When Switching Branches

Suppose you’re working on a feature and have modified some files, but you need to switch to another branch to fix a bug or work on another feature. You don’t want to commit your changes just yet, and you don’t want to lose them. This is where git stash comes in handy.

git stash           # Stash the current changes
git checkout fix    # Switch to the fix branch
# Once done with the fix, switch back to the feature branch
git checkout feature
git stash pop       # Restore the stashed changes
2. Saving Progress to Resume Later

During development, you might want to temporarily try something else or experiment with a different approach. Instead of committing your current progress, you can stash it and come back to it later when you’re ready.

git stash           # Save your current progress
# Experiment with other ideas or work on a new feature
git stash pop       # Return to the previous work later
3. Stashing Partial Changes

Sometimes, you might only want to stash certain changes while keeping others. You can use git stash to temporarily store your uncommitted changes and selectively apply them when necessary.

git stash              # Stash all changes
git stash apply stash@{0} -- open_instruct.py  # Restore changes to a specific file
4. Using Stash to Resolve Merge Conflicts

During merge operations (such as git merge or git pull), you may encounter conflicts. Before resolving these conflicts, you might want to save your current work so it’s not lost. You can stash your changes, perform the merge, and then restore the stashed changes once the merge is complete.

git stash          # Stash your progress
git pull           # Pull changes and resolve conflicts
git stash pop      # Restore your work after resolving the merge

Advanced Usage of git stash

1. Stashing Specific Files

By default, git stash will save all modified files in the working directory and staging area. If you only want to stash a specific file, use the following command:

git stash push -m "message" <file_path>

For example:

git stash push -m "Stashed changes to finetune_peft.py" open_instruct/finetune_peft.py
2. Stashing and Restoring Specific Changes

You can also stash and apply only the changes in your working directory, while keeping the staged changes intact. Use:

git stash save --keep-index  # Only stash changes in the working directory

This command will stash everything except the changes that are already staged.

Summary

git stash is a powerful tool in Git that helps you temporarily save your progress in the middle of your work, allowing you to switch tasks or branches without losing your changes. Whether you need to save your current work before switching branches, resolve conflicts, or experiment with other approaches, git stash provides a simple and effective way to manage your changes.

Common Scenarios for Using git stash:

  1. Temporarily saving your work to switch branches.
  2. Saving progress and returning to it later.
  3. Stashing partial changes.
  4. Using stash during merge conflicts.

Mastering git stash will significantly improve your workflow by allowing you to easily switch between tasks and manage your changes more efficiently.

后记

2025年2月19日12点55分于上海。在GPT4o大模型辅助下完成。

相关文章:

  • 网页制作04-html,css,javascript初认识のhtml如何使用列表
  • 【LeetCode: LCR 126. 斐波那契数 + 动态规划】
  • 内核数据结构用法(2)list
  • 开源模型应用落地-qwen2-7b-instruct-LoRA微调模型合并-LLaMA-Factory-单机多卡-RTX 4090双卡(六)
  • halcon激光三角测量(二十三)inspect_3d_surface_intersections
  • Laravel从入门到上云
  • Deepseek整合SpringAI
  • JVM 类加载器深度解析(含实战案例)
  • MySQL 安装过程记录以及安装选项详解
  • 【C++八股】内存对⻬
  • UniApp 中 margin 和 padding 属性的使用详解
  • vue3项目上线配置 nginx代理
  • hive如何导出csv格式文件
  • Bootstrap CSS 概览
  • postgres源码学习之登录
  • Qt中利用httplib调用https接口
  • Android今日头条的屏幕适配方案
  • c++进阶———继承
  • I2C学习笔记-软件模拟I2C
  • 【分治法】线性时间选择问题
  • 韩德洙成为韩国执政党总统大选候选人
  • 国家主席习近平会见斯洛伐克总理菲佐
  • 古埃及展进入百天倒计时,闭幕前168小时不闭馆
  • 《中国人民银行业务领域数据安全管理办法》发布,6月30日起施行
  • 上海:企业招用高校毕业生可享受1500元/人一次性扩岗补助
  • 甘肃省政府原副省长赵金云被决定逮捕