我想使用这个工作流:

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

有办法完成第二步吗?

例子:

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}" ###所以并不完全符合你的要求

只恢复未分阶段的变化。

其他回答

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

git stash -k

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

git stash -k -u

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

git stash pop

Git没有只存储未分阶段更改的命令。

但是,Git允许您指定要保存哪些文件。

git stash push --message 'Unstaged changes' -- app/controllers/products_controller.rb test/controllers/products_controller_test.rb

如果您只想在这些文件中保存特定的更改,请添加——patch选项。

git stash push --patch --message 'Unstaged changes' -- app/controllers/products_controller.rb test/controllers/products_controller_test.rb

——include-untracked选项允许你隐藏未跟踪的文件。

git stash push --include-untracked --message 'Untracked files' -- app/controllers/widgets_controller.rb test/controllers/widgets_controller_test.rb

运行git help stash(或man git-stash)获取更多信息。

注意:如果您的未分阶段更改相当混乱,@alesguzik的答案可能更简单。

重新思考:没有必要只将存储数据限制在工作树更改上,但是可以稍后在应用时决定只应用存储的工作树更改。

因此,在储存时间,只要像往常一样做:

git stash [-k|--keep-index]

在申请的时候

git cherry-pick -m2 -n stash

解释:-m2选择对阶段提交的第二个父元素的更改,即存储的索引状态。-n|——no-commit阻止自动提交。stash@{1}将是堆栈中第二个stash的ref…

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

当您使用

$ git stash save -keep-index

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

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

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

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

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

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

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

只恢复未分阶段的变化。

To my knowledge, it is currently impossible to save only unstaged changes in the working tree with git stash push, i.e. to save changes from the index state. This command saves all changes in the working tree (staged and unstaged changes), i.e. changes from the HEAD state, even with the option --keep-index which also sets the working tree state to the index state instead of the HEAD state (thereby creating conflicts when restoring the changes from the HEAD state with git stash pop). It would be very convenient if git stash push had an option -U|--unstaged for saving only unstaged changes (to me the option --keep-index is flawed), since it has already an option -S|--staged for saving only staged changes.

所以现在你必须模仿

git stash push --unstaged

git stash pop

使用临时文件:

git diff >unstaged
git restore .

git apply unstaged
rm unstaged

您的用例是在提交部分更改之前进行测试,它已经在参考文档中,但是使用了有缺陷的选项——keep-index,这会产生冲突。下面是带有模拟选项-U|——unstaging的版本:

git init
echo one >file
git add file
git commit
echo two >>file
git add file
echo three >>file
git diff >unstaged
git restore .
test
git commit
git apply unstaged
rm unstaged

想象状态

为了更好地理解存储,我认为在每一步查看工作树、索引和HEAD的状态是很重要的。让我们看看你的用例。

git init

working index HEAD

回显一个>文件

working index HEAD
one

Git添加文件

working index HEAD
one one

git提交

working index HEAD
one one one

回显两个>>文件

working index HEAD
one one one
two

Git添加文件

working index HEAD
one one one
two two

回三>>文件

working index HEAD
one one one
two two
three

愉快

去恢复.

working index HEAD
one one one
two two

test

git提交

working index HEAD
one one one
two two two

Git应用unstaging

罗unstaged

working index HEAD
one one one
two two two
three