我想使用这个工作流:
进行一些改变。 将未分阶段的更改保存到存储中。 用阶段中的东西做一些事情(构建、测试等)。 提交。 恢复未分阶段的更改。
有办法完成第二步吗?
例子:
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
当前回答
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.
其他回答
我对Python程序如何预提交感兴趣。这是代码。 https://github.com/pre-commit/pre-commit/blob/3fe38dff05957f609cf7b97f471b35a8d9e0659a/pre_commit/staged_files_only.py#L50
它在功能上等同于:
git diff-index --ignore-submodules --binary --exit-code --no-color --no-ext-diff $(git write-tree) -- >stash.patch
git checkout -- .
# Do stuff now
git apply stash.patch && rm stash.patch
我使用了一个别名,它接受一个字符串作为消息发送到存储条目。
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的答案可能更简单。
这是(在我看来)最好的解决方案,这完全符合OP的要求。它只存储未暂存的、被跟踪的文件——无需不必要的提交或使用——keep-index存储所有更改的文件
它列出了所有未分段的、跟踪的更改(git diff——name-only),将换行符转换为空格(| tr '\n' ' ' '),并使用git stash push存储所有这些文件:
git stash push $(git diff --name-only | tr '\n' ' ')
从Git 2.35+ (Q1 2022)开始,你现在可以在Git stash推送上使用——staging标志(man)来只stage你的索引的变化。
既然你的问题正好相反,我们有两个选择:
像这样反向操作:
git stash push --staged # Stash staged changes
git stash # Stash everything else
git stash pop stash@{1} # Restore staged changes stash
阶段性的更改是你想要保存的,而不是你想要保留的。现在你可以运行:
git stash push --staged
我从另一个S/O帖子的回答中得到了这个信息。