有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。
我做错了吗?我是否误解了git如何以其他方式简化我的过程?
有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。
我做错了吗?我是否误解了git如何以其他方式简化我的过程?
当前回答
您可以使用——staging来仅保存阶段性更改。
git stash --staged
文档:https://git-scm.com/docs/git-stash文档/ git-stash.txt-push-p——patch-S staged-k——no-keep-index-u include-untracked-a——all-q quiet-m——messageltmessagegt pathspec-from-fileltfilegt——pathspec-file-nul ltpathspecgt82308203
其他回答
铊;博士;总stash-staged
创建别名后:
git config --global alias.stash-staged '!bash -c "git stash -- \$(git diff --staged --name-only)"'
这里git diff返回的是——暂存文件的列表——只有名字 然后我们将这个列表作为pathspec传递给git stash命令。
从男人的赃物:
git stash [--] [<pathspec>...]
<pathspec>...
The new stash entry records the modified states only for the files
that match the pathspec. The index entries and working tree
files are then rolled back to the state in HEAD only for these
files, too, leaving files that do not match the pathspec intact.
另一种方法是用你不想被存储的文件创建一个临时提交,然后存储剩余的文件,轻轻地删除上次提交,保持文件完整:
git add *files that you don't want to be stashed*
git commit -m "temp"
git stash --include-untracked
git reset --soft HEAD~1
这样你只需要触摸你想要触摸的文件。
注意,“——include-untracked”在这里还用于保存新文件(这可能是您真正想要的)。
更新2022-04-21 只要使用@VonC的答案,这是对原始问题https://stackoverflow.com/a/70231955/2959469的新惯用方法
只需要为你的git <pathspec>参数添加——$(git diff——staging——name-only)
这是一个简单的一行代码:
git stash -- $(git diff --staged --name-only)
并简单地添加一条消息:
git stash push -m "My work in progress" -- $(git diff --staged --name-only)
在v2.17.1和v2.21.0.windows.1上测试
限制:
请注意,这将隐藏每一个单一的东西,如果你没有文件上演。 同样,如果你有一个文件,只是部分上演(即只有一些更改的行,被上演,而其他一些更改的行没有),那么 整个文件将被存储(包括未登台的行)。
为了达到同样的目的…
只准备您想要处理的文件。 Git提交-m 'temp' Git添加。 git藏 git重置头~1
繁荣。你不想要的文件都被藏起来了。您要的文件都准备好了。
这个回答被接受了,但太老了。现在你可以把它藏起来-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”。
享受