Git Stash解释:如何在Git中临时存储本地更改

Git has an area called the stash where you can temporarily store a snapshot of your changes without committing them to the repository. It’s separate from the working directory, the staging area, or the repository.

Git有一个称为存储区的区域,您可以在其中临时存储所做更改的快照,而无需将更改提交到存储库。 它与工作目录,暂存区或存储库分开。

This functionality is useful when you’ve made changes to a branch that you aren’t ready to commit, but you need to switch to another branch.

当您对尚未准备提交的分支进行了更改,但需要切换到另一个分支时,此功能很有用。

藏匿变化 (Stash Changes)

To save your changes in the stash, run the command:

要将更改保存在存储中,请运行以下命令:

git stash save "optional message for yourself"

This saves your changes and reverts the working directory to what it looked like for the latest commit. Stashed changes are available from any branch in that repository.

这样可以保存您的更改,并将工作目录还原为最新提交的样子。 可从该存储库中的任何分支获取已更改。

Note that changes you want to stash need to be on tracked files. If you created a new file and try to stash your changes, you may get the error No local changes to save.

请注意,您要存储的更改必须位于跟踪的文件中。 如果您创建了一个新文件并尝试隐藏您的更改,则可能会收到错误, No local changes to save

查看变更 (View Stashed Changes)

To see what is in your stash, run the command:

要查看隐藏的内容,请运行以下命令:

git stash list

This returns a list of your saved snapshots in the format stash@{0}: BRANCH-STASHED-CHANGES-ARE-FOR: MESSAGE. The stash@{0} part is the name of the stash, and the number in the curly braces ({ }) is the index of that stash. If you have multiple change sets stashed, each one will have a different index.

这将以stash@{0}: BRANCH-STASHED-CHANGES-ARE-FOR: MESSAGE格式返回已保存快照的列表stash@{0}: BRANCH-STASHED-CHANGES-ARE-FOR: MESSAGEstash@{0}部分是该stash的名称,大括号( { } )中的数字是该stash的索引。 如果您有多个变更集,每个变更集都有一个不同的索引。

If you forgot what changes were made in the stash, you can see a summary of them with git stash show NAME-OF-STASH. If you want to see the typical diff-style patch layout (with the +‘s and -‘s for line-by-line changes), you can include the -p (for patch) option. Here’s an example:

如果您忘记对存储进行了哪些更改,则可以使用git stash show NAME-OF-STASH查看它们的摘要。 如果要查看典型的差异样式修补程序布局(用+和-表示逐行更改),则可以包括-p (用于修补程序)选项。 这是一个例子:

git stash show -p stash@{0}

# Example result:
diff --git a/PathToFile/fileA b/PathToFile/fileA
index 2417dd9..b2c9092 100644
--- a/PathToFile/fileA
+++ b/PathToFile/fileA
@@ -1,4 +1,4 @@
-What this line looks like on branch
+What this line looks like with stashed changes

检索已更改 (Retrieve Stashed Changes)

To retrieve changes out of the stash and apply them to the current branch you’re on, you have two options:

要从存储中检索更改并将其应用到当前所在的分支,您有两个选择:

  1. git stash apply STASH-NAME applies the changes and leaves a copy in the stash

    git stash apply STASH-NAME应用更改并将副本git stash apply STASH-NAME在stash中

  2. git stash pop STASH-NAME applies the changes and removes the files from the stash

    git stash pop STASH-NAME应用更改并从存储中删除文件

There may be conflicts when you apply changes. You can resolve the conflicts similar to a merge (see git merge for details).

应用更改时可能会发生冲突。 您可以解决类似于合并的冲突( 有关详细信息,请参见git merge )。

删除隐藏的更改 (Delete Stashed Changes)

If you want to remove stashed changes without applying them, run the command:

如果要删除隐藏的更改而不应用它们,请运行以下命令:

git stash drop STASH-NAME

To clear the entire stash, run the command:

要清除整个存储,请运行以下命令:

git stash clear

翻译自: https://www.freecodecamp.org/news/git-stash-explained/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值