我想使用这个工作流:

进行一些改变。 将未分阶段的更改保存到存储中。 用阶段中的东西做一些事情(构建、测试等)。 提交。 恢复未分阶段的更改。

有办法完成第二步吗?

例子:

git init
echo one >file
git add file
git commit
echo two >>file
git add file
echo three >>file
git stash push
test
git commit
git stash pop

当前回答

另一个建议,与这个问题有关:

当您使用

$ git stash save -keep-index

你可能希望给stash一个消息,这样当你做一个git隐藏列表时,它会更明显地显示你之前隐藏了什么,特别是当你跟随stash操作进一步保存时。例如

$ git保存保存—keep-index“尚未进行更改”

(尽管实际上它确实包含了其他答案中提到的所有变化)。

例如,上面的语句之后可以立即加上:

$ git保存“功能X的阶段性变化”

但是要注意,你不能使用它

$ git stash apply "stash@{1}" ###所以并不完全符合你的要求

只恢复未分阶段的变化。

其他回答

这是(在我看来)最好的解决方案,这完全符合OP的要求。它只存储未暂存的、被跟踪的文件——无需不必要的提交或使用——keep-index存储所有更改的文件

它列出了所有未分段的、跟踪的更改(git diff——name-only),将换行符转换为空格(| tr '\n' ' ' '),并使用git stash push存储所有这些文件:

git stash push $(git diff --name-only | tr '\n' ' ')

使用实例添加未分期(未添加到提交)的文件到stash。

git stash -k

如果你想要将新添加的文件(不是阶段性的-不是绿色的)也包含到stash中,请执行以下操作:

git stash -k -u

然后可以提交暂存文件。在此之后,您可以使用命令返回最后存储的文件:

git stash pop

2022:我在“在git中只存储阶段性变化-这可能吗?”中提到,git 2.35(第一季度2022)附带了“git隐藏推送-阶段性”(男人):

此选项仅对推送和保存命令有效。 只保存当前暂存的更改。 这类似于基本的git提交,除了状态提交到stash而不是当前分支。


2019:该命令的现代形式是git stash push[——][<pathspec>…]],因为Git 2.16+ (Git保存已弃用)

你可以将其与通配符表单结合起来,例如:

git stash push --all --keep-index ':(glob)**/*.testextension' 

但这并不适用于Git for Windows,直到Git 2.22(2019年第二季度),见issue 2037,考虑到Git stash已在C中重新实现(而不是shell脚本)。

参见Thomas Gummerer (tummerer)的commit 7db9302(2019年3月11日)。 参见Johannes Schindelin (dscho)的commit 1366c78, commit 7b556aa(2019年3月07日)。 (由Junio C Hamano - gitster -在commit 0ba1ba4中合并,2019年4月22日)

built-in stash: handle :(glob) pathspecs again When passing a list of pathspecs to, say, git add, we need to be careful to use the original form, not the parsed form of the pathspecs. This makes a difference e.g. when calling git stash -- ':(glob)**/*.txt' where the original form includes the :(glob) prefix while the parsed form does not. However, in the built-in git stash, we passed the parsed (i.e. incorrect) form, and git add would fail with the error message: fatal: pathspec '**/*.txt' did not match any files at the stage where git stash drops the changes from the worktree, even if refs/stash has been actually updated successfully.

Git stash push有一个选项——keep-index,这正是你所需要的。

运行git stash push——keep-index。

没有阶段性变化的隐藏

——keep-index / -k的问题

在Git中只存储工作树(非阶段性更改)比它应该做的要困难得多。接受的答案,以及其他一些答案,将存储非阶段的更改,并按照请求通过——keep-index离开阶段。

然而,不明显的是——keep-index还存储了分阶段的更改。阶段性的更改最终同时存在于阶段和存储中。这很少是人们想要的,因为对隐藏的任何临时更改都可能在稍后弹出隐藏时导致冲突。

别名解决方案

这个别名可以很好地执行工作副本更改:

stash-working = "!f() { \
  git commit --quiet --no-verify -m \"temp for stash-working\" && \
  git stash push \"$@\" && \
  git reset --quiet --soft HEAD~1; }; f"

它临时提交分阶段的更改,从剩余的更改中创建一个隐藏(并允许额外的参数,如——include-untracked和——message作为别名参数传递),然后重置临时提交以获得分阶段的更改。

它类似于@Simon Knapp的答案,但有一些小的区别——它在所采取的临时操作上使用——quiet,它接受任意数量的参数用于stash推送,而不是硬编码-m,它确实在最终重置中添加——soft,以便索引保持它开始时的状态。它还在提交时使用——no-verify来避免来自预提交钩子(HT: @Granfalloner)对工作副本的更改。

关于只存储阶段性更改的相反问题(别名stash-index),请参阅此答案。