我想使用这个工作流:
进行一些改变。 将未分阶段的更改保存到存储中。 用阶段中的东西做一些事情(构建、测试等)。 提交。 恢复未分阶段的更改。
有办法完成第二步吗?
例子:
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 save --keep-index
此外,Re:
为什么不在提交更改之后提交它们呢?——心
答:因为你应该总是签入测试过的代码:)这意味着,你只需要用你即将提交的更改来运行测试
当然,作为一名有经验的程序员,您天生就有测试和检查这些更改的冲动——这只是在开玩笑
其他回答
重新思考:没有必要只将存储数据限制在工作树更改上,但是可以稍后在应用时决定只应用存储的工作树更改。
因此,在储存时间,只要像往常一样做:
git stash [-k|--keep-index]
在申请的时候
git cherry-pick -m2 -n stash
解释:-m2选择对阶段提交的第二个父元素的更改,即存储的索引状态。-n|——no-commit阻止自动提交。stash@{1}将是堆栈中第二个stash的ref…
Git stash push有一个选项——keep-index,这正是你所需要的。
运行git stash push——keep-index。
我使用了一个别名,它接受一个字符串作为消息发送到存储条目。
mystash = "!f() { git commit -m hold && git stash push -m \"$1\" && git reset HEAD^; }; f"
哪一个:
提交索引中的所有内容, 将更改的内容存储在工作树中(当然可以添加-u或-a), 将最后一次提交重置回工作尝试(可能需要使用——soft将其保留在索引中)。
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的答案可能更简单。
这可以通过3个步骤完成:保存阶段性更改,保存所有其他内容,使用阶段性更改恢复索引。基本上就是:
git commit -m 'Save index'
git stash push -u -m 'Unstaged changes and untracked files'
git reset --soft HEAD^
这正是你想要的。