git - 如何忽略 `git stash -p` 中添加的帅哥

标签 git git-stash

想象一下这个场景:

# edit two files
git add -p // add hunks from one file

现在,当您运行 git stash -p 时,它会再次询问您是否要存储刚刚通过 git add -p 选择的 hunks。有什么方法可以配置 git 默认忽略这些已经添加的 hunks 吗?大多数时候,我不想 stash 我已经添加的东西。

最佳答案

联机帮助页中有一个类似的例子:

ma​​n git stash:

"Testing partial commits
You can use git stash save --keep-index when you want 
to make two or more commits out of the changes in the 
work tree, and you want to test each change before 
committing:

# ... hack hack hack ...
$ git add --patch foo            # add just first part to the index
$ git stash save --keep-index    # save all other changes to the stash"*

我可以确认:

如果你使用 git stash -p(这意味着 --keep-index),你仍然会被问及索引中已经存在的更改是否应该藏起来(正如你所描述的)。

因此,联机帮助页似乎令人困惑,其他地方也提到了这一点:https://github.com/progit/progit2/issues/822

总结一下:

--keep-index(或 -p 表示 --keep-index)只是保持索引不变。已经上演的更改仍然会插入到存储中。 AFAIK,没有办法按照您的描述进行操作。

或者,更准确地说(同样来自联机帮助页):

With --patch, you can interactively select hunks from 
the diff between HEAD and the working tree to be stashed. 

The stash entry is constructed such that its index state 
is the same as the index state of your repository, and its 
worktree contains only the changes you selected interactively.

备选方案:

至少有 3 种方法可以实现您想要的(或多或少):

  • 不要将 -p 与 git stash 一起使用。 stash 所有东西(使用 --keep-index 和可能的 --all,以确保你已经安全地 stash 了所有东西)。
  • 在存储之前提交您的暂存更改。这样你就不会在 HEAD 和工作树之间有差异,因为你想从存储中忽略这些更改。但是,如果您还不确定要提交这个怎么办?您以后可以随时进行更改,并使用 --amend 更改现有提交。
  • 取消暂存您的更改(从索引中删除)然后存储。

关于git - 如何忽略 `git stash -p` 中添加的帅哥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50242489/

相关文章:

GIT,不要 merge 特定文件

git status 显示我在 .gitignore 文件中明确列出的文件

node.js - npm install 时发生致命坏对象错误

git - 应用 git stash 时如何忽略空格?

git - 如何在 git stash/git stash pop 之后恢复索引?

Gitlab-ci.yml 创建 merge 请求

git - 在共享更改后更改另一个分支所基于的分支

git - 将暂存文件与 stash 文件进行比较

git - 如何在每次从 master pull 后将本地更改应用到 git repo 而无需推送到 master

git - 是否可以在没有 git clone 的情况下获取远程 git repo 的提交日志/消息