有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。

我做错了吗?我是否误解了git如何以其他方式简化我的过程?


当前回答

要修剪意外的更改,特别是删除多个文件,请执行以下操作:

git add <stuff to keep> && git stash --keep-index && git stash drop

换句话说,把垃圾藏起来,把它们一起扔掉。

在git版本2.17.1中测试

其他回答

为什么不提交对某个错误的更改,并根据该提交及其前身创建一个补丁呢?

# hackhackhack, fix two unrelated bugs
git add -p                   # add hunks of first bug
git commit -m 'fix bug #123' # create commit #1
git add -p                   # add hunks of second bug
git commit -m 'fix bug #321' # create commit #2

然后,创建适当的补丁,使用git format-patch:

git format-patch HEAD^^

这将创建两个文件:0001-fix-bug-123。补丁和0002-fix-bug-321.patch

或者您可以为每个错误创建单独的分支,这样您就可以单独合并或重新建立错误修复,甚至在它们不起作用时删除它们。

我还没有见过这种不需要使用git stash的解决方案:

你甚至不需要使用git隐藏在所有。你可以使用一个专用的分支来解决这个问题(分支很便宜)。

实际上,你可以用几个连续的命令分别隔离非变化和阶段性变化,你可以把它们捆绑在一个git别名中:

创建并切换到一个新的分支,在那里你将分别提交阶段性和非阶段性的更改:参见这里

在任何时候,你都可以从创建的分支中选择-e一个提交,并将其应用到你想要的地方(-e更改其提交消息)。

当你不再需要它时,你可以删除这个“收藏分支”。你可能不得不使用-D选项来强制删除(而不是-D普通选项),因为该分支没有合并,git可能会认为如果你删除它会有丢失数据的风险。如果你没有选择删除前的提交,这是正确的:

git branch -D separated-stashes

您还可以为~/添加别名。Gitconfig,以便自动化此行为:

git config --global alias.bratisla '!git switch -c separated-stashes; git commit -m "staged changes"; git add -u; git commit -m "unstaged changes"; git switch -' # why this name ? : youtu.be/LpE1bJp8-4w

在“藏” 在“藏”



当然,您也可以使用两个连续的存储来实现相同的结果

正如在其他答案中所述,您可以使用git stash (-k|——keep-index)与其他命令组合使用一些方法来仅保存非阶段性或仅阶段性的更改。

我个人认为-k选项非常令人困惑,因为它存储了所有内容,但将阶段性更改保持在阶段性状态(这解释了为什么使用“——keep-index”)。而储存东西通常会把它移动到一个储存入口。使用-k,非暂存的更改通常会被存储,但暂存的更改只是复制到相同的暂存条目。


第0步:你的git状态中有两个东西:一个包含阶段性变化的文件,另一个包含非阶段性变化的文件。

第一步:保存非阶段性+阶段性变化,但将阶段性变化保留在索引中:

git stash -k -m "all changes"

-m "…"部分是可选的,git stash -k实际上是git stash push -k的别名(它不会远程推送任何东西,顺便说一句,不用担心),它接受一个-m选项来标记你的stash条目,以使其清晰(就像一个提交消息或标签,但对于一个stash条目)。它是已弃用的git存储保存的新版本。


步骤1bis(可选):

git stash

隐藏阶段性更改(仍在索引中)。 这一步对于下面的内容不是必需的,但是它表明,如果您愿意,您可以只将阶段性的更改放在存储条目中。 如果你使用这一行,你必须在继续第二步之前git stash (pop|apply) && git add -u。


第二步:

git commit -m "staged changes"

提交只包含从步骤0开始的阶段性更改,它包含与步骤1bis开始的隐藏条目相同的内容。


第三步:

git stash (pop|apply)

恢复第1步的存储。 注意,这个隐藏条目包含了所有内容,但是由于您已经提交了阶段性更改,所以这个隐藏只会添加从第0步开始的非阶段性更改。

注意:这里的“restore”并不是指“git restore”,这是一个不同的命令。


第四步:

git add -u

将弹出的存储的内容添加到索引中


第五步:

git commit -m "unstaged changes"

这里的“未分期”,与步骤2和步骤3注释中的“分期”一样,指的是步骤0。实际上,您正在登台并提交从步骤0开始的“登台更改”。


完成了! 您现在有两个独立的提交,其中包含从第0步开始的(un)阶段性更改。 您可能希望修改/重新设置它们以进行其他更改或重命名/删除/压缩它们。 取决于你对你的stash的堆栈做了什么(弹出或应用),你可能还想要git stash (drop|clear)它。你可以看到你用git stash (list|show)

根据你对Mike Monkiewicz的回答,我建议使用一个更简单的模型:使用常规的开发分支,但是使用合并的squash选项来在你的主分支中获得一个单一的提交:

git checkout -b bug1    # create the development branch
* hack hack hack *      # do some work
git commit
* hack hack hack *
git commit
* hack hack hack *
git commit
* hack hack hack *
git commit
git checkout master     # go back to the master branch
git merge --squash bug1 # merge the work back
git commit              # commit the merge (don't forget
                        #    to change the default commit message)
git branch -D bug1      # remove the development branch

这个过程的优点是您可以使用正常的git工作流程。

在Git 2.35 (Q1 2022)中,“Git stash”(man)学会了——staging选项来隐藏添加到索引中的内容(而不是其他内容)。

所以现在这是官方支持的(8年后)。

参见Sergey Organov (sorganov)的commit a8a6e06(2021年10月28日)和commit 41a28eb(2021年10月18日)。 (由Junio C Hamano—gitster—在commit 44ac8fd中合并,2021年11月29日)

Stash:为“推送”和“保存”执行“——staging”选项 署名:Sergey Organov

只保存已上演的更改。 这种模式允许轻松地隐藏一些与当前正在进行的工作无关的更改,以便以后重用。 与'stash push -patch'不同,- staging支持使用任何工具来选择要存储的更改,包括但不限于'git add -interactive'(man)。

Git stash现在包括在它的手册页:

“git藏”[推动[p |——补丁][s |——上演][- k |——[不——]keep-index] [q |——安静)

Git stash现在包括在它的手册页:

保存[- p |——补丁][s |——上演][- k |——[不——]keep-index] [- u |——include-untracked][——|——所有][q |——安静][<消息>]

Git stash现在包括在它的手册页:

- s ——上演了 此选项仅对推送和保存命令有效。 只保存当前暂存的更改。这类似于 基本的git提交,除了状态被提交到stash 当前分支的。 ——patch选项优先于此选项。

Git stash现在包括在它的手册页:

Saving unrelated changes for future use When you are in the middle of massive changes and you find some unrelated issue that you don't want to forget to fix, you can do the change(s), stage them, and use git stash push --staged to stash them out for future use. This is similar to committing the staged changes, only the commit ends-up being in the stash and not on the current branch. ---------------------------------------------------------------- # ... hack hack hack ... $ git add --patch foo # add unrelated changes to the index $ git stash push --staged # save these changes to the stash # ... hack hack hack, finish curent changes ... $ git commit -m 'Massive' # commit fully tested changes $ git switch fixup-branch # switch to another branch $ git stash pop # to finish work on the saved changes ----------------------------------------------------------------

Git stash -keep-index是一个很好的解决方案…除了在已删除的路径上无法正常工作,这已在Git 2.23(2019年第三季度)中得到修复

参见Thomas Gummerer (tummerer)提交b932f6a(2019年7月16日)。 (由Junio C Hamano - gitster -在commit f8aee85中合并,2019年7月25日)

stash: fix handling removed files with --keep-index git stash push --keep-index is supposed to keep all changes that have been added to the index, both in the index and on disk. Currently this doesn't behave correctly when a file is removed from the index. Instead of keeping it deleted on disk, **--keep-index currently restores the file.** Fix that behaviour by using 'git checkout' in no-overlay mode which can faithfully restore the index and working tree. This also simplifies the code. Note that this will overwrite untracked files if the untracked file has the same name as a file that has been deleted in the index.