仅在 Git 中存储未暂存的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7650797/
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 11:59:30  来源:igfitidea点击:

Stashing only un-staged changes in Git

gitgit-stash

提问by Unapiedra

I'd like to do the following work flow:

我想做以下工作流程:

  1. Add changes to the stage.
  2. Stash all the other changes that were not staged.
  3. Do some stuff with the things in stage (i.e. build, run tests, etc)
  4. Apply the stash.
  1. 向舞台添加更改。
  2. 隐藏所有其他未上演的更改。
  3. 对阶段中的事情做一些事情(即构建、运行测试等)
  4. 应用藏匿处。

Is there a way to do step 2?

有没有办法做第2步?

Example

例子

 echo "123" > foo
 git add foo # Assumes this is a git directory
 echo "456" >> foo
 git stash
 cat foo # Should yield 123

回答by vhallac

git stash savehas an option --keep-indexthat does exactly what you need.

git stash save有一个选项--keep-index可以完全满足您的需求。

So, run git stash save --keep-index.

所以,运行git stash save --keep-index

回答by alesguzik

This may be done in 3 steps: save staged changes, stash everything else, restore index with staged changes. Which is basically:

这可以通过 3 个步骤完成:保存分阶段更改,存储其他所有内容,使用分阶段更改恢复索引。这基本上是:

git commit -m 'Save index'
git stash push -u -m 'Unstaged changes and untracked files'
git reset --soft HEAD^

This will do exactly what you want.

这将完全符合您的要求。

回答by sehe

git stash save --keep-index

Also, Re:

另外,回复:

Why not commit your changes after staging them? – Shin

为什么不在暂存更改后提交更改?– 新

A: Because you should always checkin tested code :) That means, you need to run the tests with only the changes you are about to commit

A:因为你应该总是签入测试过的代码 :) 这意味着,你只需要运行你将要提交的更改的测试

All this apart from the fact that of course, as an experienced programmer, you have the innate urge to test and review just those changes -- only partlykidding

除此之外,当然,作为一名经验丰富的程序员,您天生就有测试和这些更改的冲动——这只是部分在开玩笑

回答by Eugen Konkov

With git version 2.7.4you may do:

有了git version 2.7.4你可以这样做:

git stash save --patch

The gitwill ask you to add or not your changes into stash.
And you then just answer yor n

git会要求你添加或不更改为藏匿。
然后你只需回答yn

You can restore working directory as you always do that:

您可以像往常一样恢复工作目录:

git stash pop

or, if you want to keep saved changes in stash:

或者,如果您想在 stash 中保留已保存的更改:

git stash apply

回答by Rhubbarb

Extending previous answers, I sometimes have a complex set of changes staged, but wish to commit a separate change first. For example, I might have spotted a bug or otherwise incorrect code that I'd like to fix ahead of my staged changes. One possible route to take is this:

扩展以前的答案,我有时会进行一组复杂的更改,但希望先提交单独的更改。例如,我可能已经发现了一个错误或其他不正确的代码,我想在分阶段更改之前修复这些代码。一种可能的路线是这样的:

first stash everything, but leave the staged changes intact

首先隐藏所有内容,但保持分阶段的更改完好无损

$ git stash save --keep-index [--include-untracked]

$ git stash save --keep-index [--include-untracked]

now stash the staged changes separately too

现在也分别存储分阶段的更改

$ git stash save

$ git stash 保存

make changes for fix; and test; commit them:

进行更改以修复;和测试;提交它们:

$ git add [--interactive] [--patch]

$ git commit -m"fix..."

$ git add [--interactive] [--patch]

$ git commit -m"修复..."

now restore the previously staged changes:

现在恢复先前阶段的更改:

$ git stash pop

$ git stash pop

resolve any conflicts, and note that if there were conflicts, git will have applied but notdropped that top stash entry.

解决任何冲突,并注意如果有冲突,git 将应用但不会删除顶部存储条目。

(... Then commit the staged changes, and restore the stash of all the other changes, and continue ...)

(...然后提交暂存的更改,并恢复所有其他更改的存储,然后继续...)

回答by srth12

To add the unstagged (not added to commit) files to stash, run the following command:

要将未标记(未添加到提交)的文件添加到 stash,请运行以下命令:

git stash -k

Then you can commit the staged files. After that you can get back the last stashed files using the command:

然后您可以提交暂存文件。之后,您可以使用以下命令取回最后保存的文件:

git stash pop

回答by ma11hew28

Git doesn't have a command that stashes only your unstaged changes.

Git 没有仅存储未暂存更改的命令。

Git does, however, let you specify which files you want to stash.

但是,Git 确实允许您指定要存储的文件。

git stash push --message 'Unstaged changes' -- app/controllers/products_controller.rb test/controllers/products_controller_test.rb

If you only want to stash specific changes in those files, add the --patchoption.

如果您只想隐藏这些文件中的特定更改,请添加该--patch选项。

git stash push --patch --message 'Unstaged changes' -- app/controllers/products_controller.rb test/controllers/products_controller_test.rb

The --include-untrackedoption lets you stash untracked files.

--include-untracked选项可让您隐藏未跟踪的文件。

git stash push --include-untracked --message 'Untracked files' -- app/controllers/widgets_controller.rb test/controllers/widgets_controller_test.rb

Run git help stash(or man git-stash) for more info.

运行git help stash(或man git-stash)以获取更多信息。

Note: If your unstaged changes are rather disoganized, @alesguzik's answeris probably easier.

注意:如果您的未分阶段更改相当混乱,@alesguzik 的答案可能更容易。

回答by Rhubbarb

Another tip, related to the question:

与问题相关的另一个提示:

When you effectively stash your unstaged changes using

当您使用以下方法有效地隐藏未暂存的更改时

$ git stash save --keep-index

$ git stash save --keep-index

you might wish to give the stash a message, so that when you to do a git stash listit's more obvious what you have stashed before, especially if you follow that stash operation by further saves. For example

您可能希望给 stash 一个消息,这样当您执行某个操作时,您git stash list之前存储的内容会更加明显,特别是如果您通过进一步保存来执行该 stash 操作。例如

$ git stash save --keep-index "changes not yet staged"

$ git stash save --keep-index "更改尚未上演"

(although actually it does contain all the changes as noted in other answers).

(尽管实际上它确实包含其他答案中指出的所有更改)。

For example, the above might be followed immediately by:

例如,上面可能紧跟着:

$ git stash save "staged changes for feature X"

$ git stash save“功能X的分阶段更改”

Beware, though, that you can'tthen use

但是请注意,您将无法使用

$ git stash apply "stash@{1}" ### ? doesn't quite do what you might want

$ git stash apply "stash@{1}" ### ? 并没有完全按照您的意愿行事

to restore just the unstaged changes.

仅恢复未暂存的更改。

回答by Raman

Stashing just the working tree (unstaged changes) in Git is more difficult than it should be. The accepted answer stashes the unstaged changes, but also stashes the stagedchanges (and leaves them staged as well), which is rarely what you want.

在 Git 中仅存储工作树(未暂存的更改)比应有的要困难得多。接受的答案隐藏了未分阶段的更改,但也隐藏了分阶段的更改(并使它们也分阶段),这很少是您想要的。

This alias works well:

这个别名很好用:

stash-working = "!f() { \
  git commit --quiet -m \"temp for stash-working\" && \
  git stash push \"$@\" && \
  git reset --quiet --soft HEAD~1; }; f"

It commits the staged changes temporarily, creates a stash from the remaining changes (and allows additional arguments such as --include-untrackedand --messageto be passed as alias arguments), and then resets the temporary commit to get back the staged changes.

它临时提交暂存更改,从剩余的更改中创建一个存储(并允许附加参数,例如--include-untracked--message作为别名参数传递),然后重置临时提交以取回暂存更改。

It is similar to @Simon Knapp's answer, but with a few minor differences -- it uses --quieton the temporary actions taken, and it accepts any number of parameters for the stash push, rather than hard-coding the -m, and it does add --softto the final reset so that the index remains as it started.

它类似于@Simon Knapp 的回答,但有一些细微的差别——它用于--quiet采取的临时操作,并且它接受任意数量的 stash 参数push,而不是硬编码-m,它确实添加--soft到最终重置以便索引保持它开始时的状态。

For the opposite problem of stashing just the staged changes (alias stash-index) see this answer.

对于仅存储分阶段更改(别名stash-index)的相反问题,请参阅此答案

回答by VonC

The modern form of that command is git stash push [--] [<pathspec>...], since Git 2.16+ (git stash saveis deprecated)

该命令的现代形式是git stash push [--] [<pathspec>...],因为 Git 2.16+ (git stash save已弃用

You can combine that with a wildcard form, for example:

您可以将其与通配符形式结合使用,例如:

git stash push --all --keep-index ':(glob)**/*.testextension' 

But that does not work well with Git for Windows, until Git 2.22 (Q2 2019), see issue 2037, considering git stashhas been re-implemented in C(instead of a shell script)

但这不适用于 Windows 版 Git,直到 Git 2.22(2019 年第二季度),请参阅issue 2037,考虑git stash已在 C(而不是 shell 脚本)中重新实现

See commit 7db9302(11 Mar 2019) by Thomas Gummerer (tgummerer).
See commit 1366c78, commit 7b556aa(07 Mar 2019) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster--in commit 0ba1ba4, 22 Apr 2019)

请参阅Thomas Gummerer ( ) 的commit 7db9302(11 Mar 2019 )。 请参阅Johannes Schindelin ( ) 的commit 1366c78commit 7b556aa(2019 年 3 月 7 日(由Junio C Hamano合并-- --提交 0ba1ba4,2019 年 4 月 22 日)tgummerer
dscho
gitster

built-in stash: handle :(glob)pathspecs again

When passing a list of pathspecs to, say, git add, we need to be careful to use the original form, not the parsed form of the pathspecs.

This makes a difference e.g. when calling

git stash -- ':(glob)**/*.txt'

where the original form includes the :(glob)prefix while the parsed form does not.

However, in the built-in git stash, we passed the parsed (i.e. incorrect) form, and git addwould fail with the error message:

fatal: pathspec '**/*.txt' did not match any files

at the stage where git stashdrops the changes from the worktree, even if refs/stashhas been actually updated successfully.

内置stash:(glob)再次处理路径规范

例如,当将路径规范列表传递给 时git add,我们需要小心使用原始形式,而不是路径规范的解析形式。

这会有所不同,例如在调用时

git stash -- ':(glob)**/*.txt'

其中原始形式包括:(glob)前缀而解析形式不包括。

但是,在内置的 中git stash,我们传递了解析的(即不正确的)表单,并且git add会失败并显示错误消息:

fatal: pathspec '**/*.txt' did not match any files

git stash从工作树中删除更改的阶段,即使refs/stash实际上已成功更新。