Git stash uncached:如何收起所有未暂存的更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20028507/
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
Git stash uncached: how to put away all unstaged changes?
提问by klm123
Suppose two set of changes are made in a project versioned by git. One set is staged and the other is not.
假设在由 git 版本化的项目中进行了两组更改。一套是上演的,另一套不是。
I would like to recheck staged changes by running my project at this state (before commit). What is a simple way to put away all unstaged changes and leave only staged?So I need unstaged changes to disappear from my project, but to be stored somewhere for further work.
我想通过在此状态(提交前)运行我的项目来重新检查阶段性更改。什么是放弃所有未暂存更改并仅保留暂存的简单方法?因此,我需要未暂存的更改从我的项目中消失,但要存储在某个地方以供进一步工作。
This is sounds very much like git stash
command. But git stash
would put both unstaged and staged changes away from my project. And I can't find something like git stash uncached
.
这听起来很像git stash
命令。但是git stash
会将未分阶段和分阶段的更改从我的项目中删除。而且我找不到类似的东西git stash uncached
。
回答by stephen.hanson
The accepted answer also stashes staged changes as a few have pointed out, and it doesn't stash untracked files. Here's a way to do it without getting your staged changes in the stash, while also removing and stashing untracked files.
正如一些人指出的那样,接受的答案还隐藏了分阶段的更改,并且它不会隐藏未跟踪的文件。这是一种无需在存储中进行分阶段更改的方法,同时还可以删除和隐藏未跟踪的文件。
The idea is to do a temporary commit of your staged changes, then stash the unstaged changes, then un-commit the temp commit:
这个想法是对您的暂存更改进行临时提交,然后存储未暂存的更改,然后取消提交临时提交:
# temp commit of your staged changes:
$ git commit --message "WIP"
# stage your previously unstaged files before stashing (so you get untracked files):
$ git add .
$ git stash
# now un-commit your WIP commit:
$ git reset --soft HEAD^
At this point, you'll have a stash of your unstaged changes and will only have your staged changes present in your working copy.
此时,您将拥有未暂存更改的存储库,并且只会在工作副本中显示暂存更改。
回答by Mohammad AbuShady
Update 2:
I'm not sure why people are complaining about this answer, it seems to be working perfectly with me, for the untracted files you can add the -u
flag
更新 2:
我不知道为什么人们抱怨这个答案,它似乎与我完美配合,对于未提取的文件,您可以添加-u
标志
The full command becomes git stash --keep-index -u
完整的命令变成 git stash --keep-index -u
And here's a snippet from the git-stash
help
这是git-stash
帮助中的一个片段
If the --keep-index option is used, all changes already added to the index are left intact.
If the --include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean, leaving the working directory in a very clean state. If the --all option is used instead then the ignored files are stashed and cleaned in addition to the untracked files.
如果使用 --keep-index 选项,则已添加到索引的所有更改都保持不变。
如果使用 --include-untracked 选项,所有未跟踪的文件也会被隐藏,然后用 git clean 清理,使工作目录处于非常干净的状态。如果改为使用 --all 选项,那么除了未跟踪的文件之外,还会隐藏和清理被忽略的文件。
And this is a gif of how it looks:
这是它的外观的 gif:
Update:
更新:
I tested my answer again today (31/1/2020) against git version 2.24.0
, and I still believe that it's correct, I added a small note above about the untracked files.
If you think it's not working please also mention your git version.
我今天(31/1/2020)针对 git version 再次测试了我的答案2.24.0
,我仍然相信它是正确的,我在上面添加了一个关于未跟踪文件的小注释。如果您认为它不起作用,请同时提及您的 git 版本。
Old answer:
If the --keep-index
option is used, all changes already added to the index are left intact:
旧答案:
如果使用该--keep-index
选项,则已添加到索引的所有更改都保持不变:
git stash --keep-index
From the documentation of 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 $ edit/build/test first part $ git commit -m 'First part' # commit fully tested change $ git stash pop # prepare to work on all other changes # ... repeat above five steps until one commit remains ... $ edit/build/test remaining parts $ git commit foo -m 'Remaining parts'
测试部分提交
git stash save --keep-index
当您想从工作树中的更改中进行两次或多次提交,并且您想在提交之前测试每个更改时,您可以使用:# ... 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 $ edit/build/test first part $ git commit -m 'First part' # commit fully tested change $ git stash pop # prepare to work on all other changes # ... repeat above five steps until one commit remains ... $ edit/build/test remaining parts $ git commit foo -m 'Remaining parts'
But, if you just want to visually check the staged changes only, you can try difftool
:
但是,如果您只想直观地检查分阶段的更改,您可以尝试difftool
:
git difftool --cached
回答by Binary Phile
I found the marked answer did not work for me since I needed something which truly stashed only my unstaged changes. The marked answer, git stash --keep-index
, stashes both the staged and unstaged changes. The --keep-index
part merely leaves the index intact on the working copy as well. That works for OP, but only because he asked a slightly different question than he actually wanted the answer for.
我发现标记的答案对我不起作用,因为我需要一些真正只隐藏我未暂存更改的东西。标记的答案git stash --keep-index
隐藏了已暂存和未暂存的更改。该--keep-index
部分也只是在工作副本上保持索引完好无损。这对 OP 有效,但这只是因为他提出的问题与他实际想要的答案略有不同。
The only true way I've found to stash unstaged changes is to not use the stash at all:
我发现存储未暂存更改的唯一正确方法是根本不使用存储:
git diff > unstaged.diff
git apply -R unstaged.diff
git checkout -- .
will also work instead of apply -R
.
git checkout -- .
也可以代替apply -R
.
Work work work...
工作工作工作...
git apply unstaged.diff
rm unstaged.diff
回答by Cory Danielson
Git: Stash unstaged changes
Git:隐藏未暂存的更改
This will stash all modifications that you did not git add:
这将隐藏您没有 git add 的所有修改:
git stash -k
Note that newly created (and non-added) files will remain in your working directory unless you also use the -u
switch.
请注意,除非您也使用该-u
开关,否则新创建(和未添加)的文件将保留在您的工作目录中。
git stash -k -u
Also, your working directory must be clean (i.e. all changes need to be added) when you git stash pop later on.
此外,当您稍后 git stash pop 时,您的工作目录必须是干净的(即需要添加所有更改)。
http://makandracards.com/makandra/853-git-stash-unstaged-changes
http://makandracards.com/makandra/853-git-stash-unstaged-changes