假设在一个由 git 版本控制的项目中进行了两组更改。一组已上演,另一组未上演。
我想通过在此状态(提交前)运行我的项目来重新检查阶段性更改。 有什么简单的方法可以放弃所有未暂存的更改,只留下暂存的?所以我需要未暂存的更改从我的项目中消失,但要存储在某个地方以供进一步工作。
这听起来很像 git stash
命令。但是 git stash
会将未暂存和已暂存的更改从我的项目中移除。而且我找不到像 git stash uncached
这样的东西。
最佳答案
更新 2:
我不确定为什么人们会提示这个答案,它似乎对我来说很完美,对于未处理的文件,您可以添加 -u
标志
完整命令变为 git stash --keep-index -u
这是 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.
这是它的外观的 gif 动图:
更新:
<删除> 尽管这是选定的答案,但很多人指出[下面的答案](https://stackoverflow.com/a/34681302/292408) 是正确的,我建议您检查一下。我今天 (31/1/2020) 针对 git version 2.24.0
再次测试了我的答案,我仍然相信它是正确的,我在上面添加了一个关于未跟踪文件的小注释。
如果您认为它不起作用,请同时提及您的 git 版本。
旧答案:
如果使用 --keep-index
选项,所有已添加到索引的更改将保持不变:
git stash --keep-index
来自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'
但是,如果您只想直观地检查阶段性更改,您可以尝试 difftool
:
git difftool --cached
关于Git 存储未缓存 : how to put away all unstaged changes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20028507/