有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。
我做错了吗?我是否误解了git如何以其他方式简化我的过程?
有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。
我做错了吗?我是否误解了git如何以其他方式简化我的过程?
当前回答
为了达到同样的目的…
只准备您想要处理的文件。 Git提交-m 'temp' Git添加。 git藏 git重置头~1
繁荣。你不想要的文件都被藏起来了。您要的文件都准备好了。
其他回答
更新2022年1月:Git 2.35已经发布,stash现在支持——staging参数。因此,这个答案在Git 2.35+中已经过时了。参见vonc的回答:https://stackoverflow.com/a/70231955/430128。
旧的回答:
在Git中仅存储索引(分阶段更改)比想象中要困难得多。我发现@Joe的答案工作得很好,并把它的一个小变化变成了这个别名:
stash-index = "!f() { \
! git diff --cached --exit-code --quiet && \
git stash push --quiet --keep-index -m \"temp for stash-index\" && \
git stash push \"$@\" && \
git stash pop --quiet stash@{1} && \
git stash show -p | git apply -R; }; f"
It:
Validates that there are actually staged changes (git diff --cached --exit-code returns a non-zero status if there are). HT: @nandilugio It pushes both the staged and unstaged changes into a temporary stash, leaving the staged changes alone. It then pushes the staged changes into the stash, which is the stash we want to keep. Arguments passed to the alias, such as --message "whatever" will be added to this stash command. It pops the temporary stash to restore the original state and remove the temporary stash, and then Finally "removes" the stashed changes from the working directory via a reverse patch application.
对于相反的问题,只存储非分期的更改(别名存储工作),请参阅以下答案。
在这个场景中,我更喜欢为每个问题创建新的分支。我使用了一个前缀temp/,所以我知道我可以稍后删除这些分支。
git checkout -b temp/bug1
准备修复bug1的文件并提交它们。
git checkout -b temp/bug2
然后,您可以根据需要从相应的分支中选择提交,并提交一个拉取请求。
这个回答被接受了,但太老了。现在你可以把它藏起来-S见https://stackoverflow.com/a/70231955/683157
最初的回答: 是的,这是可能的DOUBLE STASH
准备好你需要藏匿的所有文件。 运行git stash——keep-index。这个命令将创建一个包含所有更改(阶段性和非阶段性)的存储,但是将阶段性更改保留在工作目录中(仍然处于阶段性状态)。 运行git stash push -m "good stash"(添加-u标志以在stash中包含新文件) 现在你的“好收藏”只有阶段性文件。
现在,如果你需要非暂存文件在stash之前,只需应用第一个stash(一个创建的——keep-index),现在你可以删除文件,你储存到“良好的stash”。
享受
要修剪意外的更改,特别是删除多个文件,请执行以下操作:
git add <stuff to keep> && git stash --keep-index && git stash drop
换句话说,把垃圾藏起来,把它们一起扔掉。
在git版本2.17.1中测试
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.