git 从 stash 中删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22722575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:56:26  来源:igfitidea点击:

git remove file from stash

gitgit-stashmerge-conflict-resolution

提问by

I have a stash with a bunch of files in it.

我有一个藏匿处,里面有一堆文件。

But I can't apply my stash because of a conflicting file. I've identified the problematic file in my stash and I want to remove it.

但是由于文件冲突,我无法应用我的存储。我在我的存储中发现了有问题的文件,我想删除它。

How can I remove a single file from a stash without destroying the entire thing?

如何在不破坏整个文件的情况下从存储中删除单个文件?

采纳答案by torek

A stash is a commit (or really, two or even sometimes three commits) and you cannot change a commit. The literal answer to your question, then, is "you can't". Fortunately, you don't needto.

stash 是一次提交(或者实际上是两次甚至有时是三次提交)并且您不能更改一次提交。那么,你问题的字面答案是“你不能”。幸运的是,你并不需要到。

You say you can't apply your stash because of a conflicting file. But you canapply it, you just get a merge conflict. All you need to do is resolve the merge conflict.

您说由于文件冲突而无法应用您的存储。但是你可以应用它,你只会遇到合并冲突。您需要做的就是解决合并冲突。

Let's say the conflict is in file README.txt, just so there's something to write about here.

假设冲突在 file 中README.txt,所以这里有一些东西要写。

If you want to resolve it by keeping the on-branch version, apply the stash, then check out the on-branch version to resolve the conflict:

如果您想通过保留分支版本来解决它,请应用存储,然后查看分支版本以解决冲突:

git stash apply
git checkout --ours -- README.txt  # or git checkout HEAD -- README.txt

If you want to keep the in-stash version, extract that one:

如果要保留 in-stash 版本,请提取该版本:

git checkout --theirs -- README.txt # or git checkout stash -- README.txt

Or, use any old merge resolution tool (I just use a text editor), and then "git add" the result.

或者,使用任何旧的合并解析工具(我只使用文本编辑器),然后“git add”结果。

Once you are all done with the stash, git stash dropwill "forget" the commits that make up the stash. (Don't do this until you are sure you are done with it; it's very hard to get it back afterward.)

完成所有存储后,git stash drop将“忘记”构成存储的提交。(在你确定你已经完成之前不要这样做;之后很难恢复它。)

回答by Dmytro Sirenko

There is a workaround for this situation:

这种情况有一个解决方法:

  1. save your stash as a patch file:

    $ git stash show -p > stash.patch
    
  2. apply this patch, skipping the conflicts (you will be asked for resolution of conflicts, just skip missing files):

    $ patch -p1 < stash.patch
    
  1. 将您的存储保存为补丁文件:

    $ git stash show -p > stash.patch
    
  2. 应用此补丁,跳过冲突(您将被要求解决冲突,只需跳过丢失的文件):

    $ patch -p1 < stash.patch
    

Don't forget to clean up stash.patchafterwards!

stash.patch之后别忘了清理!

回答by Francisco Aguilera

  1. Stash your uncommitted changes.
  2. Apply your original stash
  3. Discard the file
  4. 4 Apply the stash containing your uncommitted changes.
  1. 隐藏您未提交的更改。
  2. 应用您的原始存储
  3. 丢弃文件
  4. 4 应用包含未提交更改的存储。

回答by viper03

delete the required file from your local codebase and then push it to stash. The changes will get reflected and your files will be deleted from stash.

从本地代码库中删除所需的文件,然后将其推送到 stash。更改将得到反映,您的文件将从存储中删除。