我想使用这个工作流:
进行一些改变。 将未分阶段的更改保存到存储中。 用阶段中的东西做一些事情(构建、测试等)。 提交。 恢复未分阶段的更改。
有办法完成第二步吗?
例子:
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 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 [-k|--keep-index]
在申请的时候
git cherry-pick -m2 -n stash
解释:-m2选择对阶段提交的第二个父元素的更改,即存储的索引状态。-n|——no-commit阻止自动提交。stash@{1}将是堆栈中第二个stash的ref…
其他回答
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 |
我使用了一个别名,它接受一个字符串作为消息发送到存储条目。
mystash = "!f() { git commit -m hold && git stash push -m \"$1\" && git reset HEAD^; }; f"
哪一个:
提交索引中的所有内容, 将更改的内容存储在工作树中(当然可以添加-u或-a), 将最后一次提交重置回工作尝试(可能需要使用——soft将其保留在索引中)。
git stash save --keep-index
此外,Re:
为什么不在提交更改之后提交它们呢?——心
答:因为你应该总是签入测试过的代码:)这意味着,你只需要用你即将提交的更改来运行测试
当然,作为一名有经验的程序员,您天生就有测试和检查这些更改的冲动——这只是在开玩笑
Git stash push有一个选项——keep-index,这正是你所需要的。
运行git stash push——keep-index。
扩展前面的回答,我有时会有一组复杂的更改,但希望先提交一个单独的更改。例如,我可能发现了一个错误或其他不正确的代码,我想在进行阶段性更改之前修复它。一个可行的方法是:
首先把所有东西都藏起来,但保留阶段性的变化
$ git保存——keep-index[——include-untracked]
现在也将阶段性更改单独保存
$ git保存
为解决问题做出改变;和测试;提交:
$ git add[——interactive][——patch] $ git commit -m"fix…"
现在恢复之前的更改:
$ git隐藏pop
解决任何冲突,并注意如果存在冲突,git将应用而不是删除顶部的隐藏条目。
(…然后提交分阶段的更改,并恢复所有其他更改的存储,然后继续…)