如何仅存储已更改的多个文件中的一个文件?
如何在我的分支上仅存储多个更改的文件之一?
How do I stash only one of the multiple changed files on my branch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在我的分支上仅存储多个更改的文件之一?
How do I stash only one of the multiple changed files on my branch?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
-p
让你选择应该隐藏的帅哥;也可以选择整个文件。系统会提示您对每个大块进行一些操作:
-p
let's you select the hunks that should be stashed; whole files can be selected as well.You'll be prompted with a few actions for each hunk:
免责声明:以下答案适用于 git 2.13 之前的 git。对于 git 2.13 及更高版本,请查看下面的另一个答案。
警告
正如评论中所指出的,这会将所有内容放入存储中,包括暂存的和未暂存的。 --keep-index 在存储完成后只保留索引。当您稍后弹出存储时,这可能会导致合并冲突。
这将隐藏您之前未添加的所有内容。只需
git add
你想要保留的东西,然后运行它。例如,如果您想将旧提交拆分为多个变更集,可以使用以下过程:
git rebase -i
edit.
git reset HEAD^
git add <您想要在此更改中保留的文件>
git stash --keep-index
git add
任何更改。git commit
git stash pop
git rebase --继续
Disclaimer: the following answer is for git before git 2.13. For git 2.13 and over, check out another answer further down.
Warning
As noted in the comments, this puts everything into the stash, both staged and unstaged. The --keep-index just leaves the index alone after the stash is done. This can cause merge conflicts when you later pop the stash.
This will stash everything that you haven't previously added. Just
git add
the things you want to keep, then run it.For example, if you want to split an old commit into more than one changeset, you can use this procedure:
git rebase -i <last good commit>
edit
.git reset HEAD^
git add <files you want to keep in this change>
git stash --keep-index
git add
any changes.git commit
git stash pop
git rebase --continue
从 Git 2.13(2017 年第 2 季度)开始,您可以使用
git 存储推送
:简化示例:
测试用例此功能显示了更多选项:
原始答案(如下,2010 年 6 月)是关于手动选择您想要隐藏的内容。
Casebash 评论:
bukzor 的答案(已投票,2011 年 11 月)提出了一个更实用的解决方案,基于上
git add
+git stash --keep-index
。去看看并投票他的答案,这应该是官方的答案(而不是我的)。
关于该选项, chhh 在评论中指出了替代工作流程:
(原始答案 2010 年 6 月:手动存储)
然而,
git stash save --patch
可以让您实现您想要的部分存储:但是,这将保存完整索引(这可能不是您想要的,因为它可能包括已索引的其他文件)和部分工作树(可能看起来像您想要隐藏的工作树)。
可能更合适。
如果
--patch
不起作用,手动过程可能是:对于一个或多个文件,中间解决方案是:
(实际上,eleotlecram 提出了一个有趣的替代方案)
git stash
git stash
# 这次,只保存您想要的文件git stash pop stash@{1}
# 重新应用所有文件修改git checkout -- afile
# 在进行任何本地修改之前,将文件重置为 HEAD 内容在这个相当繁琐的过程结束时,您将只得到隐藏一个或多个文件。
正如 mirekphd 在 评论:
Since Git 2.13 (Q2 2017), you can stash individual files, with
git stash push
:Simplified example:
The test case for this feature shows a few more options off:
The original answer (below, June 2010) was about manually selecting what you want to stash.
Casebash comments:
bukzor's answer (upvoted, November 2011) suggests a more practical solution, based on
git add
+git stash --keep-index
.Go see and upvote his answer, which should be the official one (instead of mine).
About that option, chhh points out an alternative workflow in the comments:
(Original answer June 2010: manual stash)
Yet,
git stash save --patch
could allows you to achieve the partial stashing you are after:However that will save the full index (which may not be what you want since it might include other files already indexed), and a partial worktree (which could look like the one you want to stash).
might be a better fit.
If
--patch
doesn't work, a manual process might:For one or several files, an intermediate solution would be to:
(Actually, eleotlecram proposes an interesting alternative)
git stash
git stash
# this time, only the files you want are stashedgit stash pop stash@{1}
# re-apply all your files modificationsgit checkout -- afile
# reset the file to the HEAD content, before any local modificationsAt the end of that rather cumbersome process, you will have only one or several files stashed.
As noted by mirekphd in the comments:
使用
git stash push
,如下所示:例如:
此功能自 2017 年春季发布的 Git 2.13 起可用。
Use
git stash push
, like this:For example:
This is available since Git 2.13, released in spring 2017.
当
git stash -p
(或git add -p
和stash --keep-index
)太麻烦时,我发现更容易使用diff
、checkout
和apply
:仅“存储”特定文件/目录:
然后
When
git stash -p
(orgit add -p
withstash --keep-index
) would be too cumbersome, I found it easier to usediff
,checkout
andapply
:To "stash" a particular file/dir only:
Then afterwards
如果您不想指定包含隐藏更改的消息,请在双破折号后传递文件名。
如果它是未跟踪/新文件,您必须先暂存它。
此方法适用于 git 版本 2.13+
If you do not want to specify a message with your stashed changes, pass the filename after a double-dash.
If it's an untracked/new file, you will have to stage it first.
This method works in git versions 2.13+
假设您有 3 个文件
,并且您只想存储 b.rb 和 c.rb,但不想存储 a.rb,
您可以执行以下操作
,您就完成了!
HTH。
Let's say you have 3 files
and you want to stash only b.rb and c.rb but not a.rb
you can do something like this
And you are done!
HTH.
如果您只想存储一些已更改的文件,只需在Stage中添加其他文件,然后执行
git stash push --keep-index
它将存储所有 >取消暂存更改的文件
If you want to stash only some of changed files, simply just Add other files in the Stage, Then execute
git stash push --keep-index
It will stash all unstaged changed files
另一种方法是:
在我(再次)来到此页面并且不喜欢前两个答案(第一个答案没有回答问题并且我不太喜欢与
-p
交互模式)。这个想法与 @VonC 建议使用存储库外部的文件相同,您将所需的更改保存在某处,删除存储中不需要的更改,然后重新应用您移出的更改。然而,我使用 git stash 作为“某处”(因此,最后有一个额外的步骤:删除您放入存储中的更改,因为您也将它们移开了)。
Another way to do this:
I came up with this after I (once again) came to this page and didn't like the first two answers (the first answer just doesn't answer the question and I didn't quite like working with the
-p
interactive mode).The idea is the same as what @VonC suggested using files outside the repository, you save the changes you want somewhere, remove the changes you don't want in your stash, and then re-apply the changes you moved out of the way. However, I used the git stash as the "somewhere" (and as a result, there's one extra step at the end: removing the cahnges you put in the stash, because you moved these out of the way as well).
您可以简单地执行此操作:
或使用可选消息
You can simply do this:
or with an optional message
更新(2/14/2015) - 我稍微重写了脚本,以更好地处理冲突的情况,现在应该以未合并的冲突而不是 .rej 文件的形式呈现。
我经常发现与 @bukzor 的方法相反的做法更直观。也就是说,暂存一些更改,然后仅存储那些暂存的更改。
不幸的是,git 不提供 git stash --only-index 或类似的功能,所以我编写了一个脚本来执行此操作。
您可以将上述脚本保存为路径中某处的 git-stash-index ,然后可以将其调用为 git stash-index
现在,存储包含一个新条目,其中仅包含您暂存的更改,并且您的工作树仍然包含任何未暂存的更改。
在某些情况下,工作树的更改可能取决于索引的更改,因此当您存储索引更改时,工作树的更改会发生冲突。在这种情况下,您将遇到常见的未合并冲突,可以使用 git merge/git mergetool/etc 来解决。
Update (2/14/2015) - I've rewritten the script a bit, to better handle the case of conflicts, which should now be presented as unmerged conflicts rather than .rej files.
I often find it more intuitive to do the inverse of @bukzor's approach. That is, to stage some changes, and then stash only those staged changes.
Unfortunately, git doesn't offer a git stash --only-index or similar, so I whipped up a script to do this.
You can save the above script as
git-stash-index
somewhere on your path, and can then invoke it as git stash-indexNow the stash contains a new entry that only contains the changes you had staged, and your working tree still contains any unstaged changes.
In some cases, the working tree changes may depend on the index changes, so when you stash the index changes, the working tree changes have a conflict. In this case, you'll get the usual unmerged conflicts that you can resolve with git merge/git mergetool/etc.
由于在 Git 中创建分支很简单,您只需创建一个临时分支并将各个文件签入其中即可。
Since creating branches in Git is trivial you could just create a temporary branch and check the individual files into it.
以防万一您实际上是在使用 git stash 时放弃更改(并且不真正使用 git stash 暂时存储它),在这种情况下您可以使用
[< strong>注意]
git stash
只是分支和执行操作的更快、更简单的替代方案。Just in case you actually mean discard changes whenever you use
git stash
(and don't really use git stash to stash it temporarily), in that case you can use[NOTE]
That
git stash
is just a quicker and simple alternative to branching and doing stuff.将以下代码保存到文件中,例如,名为
stash
。用法是stash
。参数是文件完整路径的正则表达式。例如,要存储 a/b/c.txt、stash a/b/c.txt
或stash .*/c.txt
等。要复制到的代码文件:
Save the following code to a file, for example, named
stash
. Usage isstash <filename_regex>
. The argument is the regular expression for the full path of the file. For example, to stash a/b/c.txt,stash a/b/c.txt
orstash .*/c.txt
, etc.Code to copy into the file:
VonC 将文件复制到 Git 存储库外部的“中间”解决方案的问题是,您会丢失路径信息,这使得稍后将一堆文件复制回来会有些麻烦。
A 发现使用 tar (类似的工具可能会这样做)而不是复制更容易:
The problem with VonC's `intermediate' solution of copying files to outside the Git repo is that you lose path information, which makes copying a bunch of files back later on somewhat of a hassle.
A find it easier to use tar (similar tools will probably do) instead of copy:
我会使用 git stash save --patch 。我不认为交互性很烦人,因为其中有一些选项可以将所需的操作应用于整个文件。
I would use
git stash save --patch
. I don't find the interactivity to be annoying because there are options during it to apply the desired operation to entire files.目前(2024 年),Git 使用新的
--staged
选项提供了一种更简单的方法来实现此目的:令我惊讶的是,在所有答案中没有人提供最新 Git 版本的解决方案(>= 2.35)了解如何仅暂存许多未暂存更改中的特定文件。
Currently (2024), Git is providing a much simpler way to achieve this using the new
--staged
option:To my suprise, among all the answers nobody provided the solution for recent Git versions (>= 2.35) on how to stage only specific files out of many unstaged changes.
有时,在提交之前,我在分支上进行了不相关的更改,并且我想将其移动到另一个分支并单独提交(例如 master)。我这样做:
注意第一个
stash
&stash pop
可以被消除,您可以在签出时将所有更改转移到master
分支,但前提是不存在冲突。此外,如果您要为部分更改创建新分支,您将需要存储。假设没有冲突并且没有新分支,您可以简化它:
甚至不需要存储......
Sometimes I've made an unrelated change on my branch before I've committed it, and I want to move it to another branch and commit it separately (like master). I do this:
Note the first
stash
&stash pop
can be eliminated, you can carry all of your changes over to themaster
branch when you checkout, but only if there are no conflicts. Also if you are creating a new branch for the partial changes you will need the stash.You can simplify it assuming no conflicts and no new branch:
Stash not even needed...
使用 SourceTree 只需 3 个步骤即可轻松完成此操作。
这一切都可以在 SourceTree 中在几秒钟内完成,您只需单击要添加的文件(甚至单独的行)即可。添加后,只需将它们提交到临时提交即可。接下来,单击复选框添加所有更改,然后单击隐藏以隐藏所有内容。将隐藏的更改移开后,浏览一下提交列表并记下临时提交之前提交的哈希值,然后运行“git reset hash_b4_temp_commit”,这基本上就像通过将分支重置为“弹出”提交一样在它之前提交。现在,你只剩下那些你不想藏起来的东西了。
This can be done easily in 3 steps using SourceTree.
This can all be done in a matter of seconds in SourceTree, where you can just click on the files (or even individual lines) you want to add. Once added, just commit them to a temporary commit. Next, click the checkbox to add all changes, then click stash to stash everything. With the stashed changes out of the way, glance over at your commit list and note the hash for the commit before your temporary commit, then run 'git reset hash_b4_temp_commit', which is basically like "popping" the commit by resetting your branch to the commit right before it. Now, you're left with just the stuff you didn't want stashed.
这里的每个答案都如此复杂......
这个“隐藏”怎么样:
这将文件更改弹出:
与隐藏一个文件并将其弹出回来的行为完全相同。
Every answer here is so complicated...
What about this to "stash":
This to pop the file change back:
Exact same behavior as stashing one file and popping it back in.
我已经查看了此主题以及许多类似主题的答案和评论。请注意,以下命令都不正确,无法存储任何特定的跟踪/未跟踪文件:
git stash -p (--patch)
: select手动汉克斯,排除未跟踪的文件git stash -k (--keep-index)
:存储所有跟踪/未跟踪的文件并将它们保存在工作目录git stash -u (--include- untracked)
: stash 所有跟踪/未跟踪的文件git stash -p (--patch) -u (--include-untracked)
: invalid command目前最合理的方法存储任何特定的跟踪/未跟踪文件的方法是:
< strong>我在回答另一个问题时为此过程编写了一个简单的脚本,并且有步骤在此处执行 SourceTree 中的过程。
I've reviewed answers and comments for this and a number of similar threads. Be aware that none of the following commands are correct for the purpose of being able to stash any specific tracked/untracked files:
git stash -p (--patch)
: select hunks manually, excluding untracked filesgit stash -k (--keep-index)
: stash all tracked/untracked files and keep them in the working directorygit stash -u (--include-untracked)
: stash all tracked/untracked filesgit stash -p (--patch) -u (--include-untracked)
: invalid commandCurrently, the most reasonable method to be able to stash any specific tracked/untracked files is to:
I wrote a simple script for this procedure in an answer to another question, and there are steps for performing the procedure in SourceTree here.
当您尝试在两个分支之间切换时,就会发生这种情况。
尝试使用“
git add filepath
”添加文件。稍后执行这一行
When you try to switch between two branches, this situation occurs.
Try to add the files using "
git add filepath
".Later execute this line
解决方案
本地更改:
要创建仅包含 file_C 上的更改的存储“my_stash”:
完成。
解释
您可以在步骤之间使用 git status 来查看发生了什么。
Solution
Local changes:
To create a stash "my_stash" with only the changes on file_C:
Done.
Explanation
You can use git status between the steps to see what is going on.
要存储单个文件,请使用 git stash --patch [file] 。
这将提示:
Stash this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?
。只需输入a
(将此块和所有后续块存储在文件中)就可以了。To stash a single file use
git stash --patch [file]
.This is going to prompt:
Stash this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?
. Just typea
(stash this hunk and all later hunks in the file) and you're fine.类似的情况。做了承诺并意识到这是不行的。
根据答案这对我有帮助。
Similar situation. Did commit and realized it's not ok.
Based on the answers this helped me.
在这种情况下,我
git add -p
(交互式),git commit -m blah
,然后在必要时隐藏剩下的内容。In this situation I
git add -p
(interactive),git commit -m blah
and then stash what's left if necessary.我不知道如何在命令行上执行此操作,只能使用 SourceTree。假设您已更改文件 A,并在文件 B 中有两个更改块。如果您只想将第二个块存储在文件 B 中并保持其他所有内容不变,请执行以下操作:
I don't know how to do it on command line, only using SourceTree. Lets say you have changed file A, and have two change hunks in file B. If you want to stash only the second hunk in file B and leave everything else untouched, do this:
我没有找到我需要的答案,这很简单:
这仅存储一个文件。
I found no answer to be what I needed and that is as easy as:
This stashes exactly one file.
一种复杂的方法是首先提交所有内容:
重置回原始提交,但从新提交中检出 the_one_file:
现在您可以存储 the_one_file:
通过将提交的内容保存在文件系统中同时重置回原始提交进行清理:
是的,有点尴尬……
One complicated way would be to first commit everything:
Reset back to the original commit but checkout the_one_file from the new commit:
Now you can stash the_one_file:
Cleanup by saving the committed content in your file system while resetting back to the original commit:
Yeah, somewhat awkward...