我对一个文件进行了更改,再加上一个新文件,我想在切换到另一个任务时使用git stash将它们保存起来。但是git stash本身只存储对现有文件的更改;新文件仍保留在我的工作树中,打乱了我未来的工作。我如何保存这个未跟踪的文件?
当前回答
将文件添加到索引:
git add path/to/untracked-file
git stash
索引的全部内容,加上对现有文件的任何未标记的更改,都将被放入存储库。
其他回答
在VS 2019和更高版本中提供Stashing。
转到Git更改窗口Ctrl+Alt+F7现在按下“全部提交”或“提交阶段”按钮附近的下拉键,查看隐藏选项
如果您想隐藏未跟踪的文件,如Git忽略的文件或未包含在项目中的文件,请选择此选项
阅读以下答案,了解Visual studio中Git存储的完整用法:https://stackoverflow.com/a/69905607/4391394
将文件添加到索引:
git add path/to/untracked-file
git stash
索引的全部内容,加上对现有文件的任何未标记的更改,都将被放入存储库。
我认为这可以通过告诉git文件存在来解决,而不是将文件的所有内容提交到暂存区,然后调用git stash。Araqnid描述了如何实现前者。
git add --intent-to-add path/to/untracked-file
or
git update-index --add --cacheinfo 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 path/to/untracked-file
然而,后者不起作用:
$ git stash
b.rb: not added yet
fatal: git-write-tree: error building trees
Cannot save the current index state
正如其他地方所说,答案是git添加文件。例如。:
git add path/to/untracked-file
git stash
然而,问题也在另一个答案中提出:如果你真的不想添加文件怎么办?好吧,据我所知,你必须这样做。而且以下方法行不通:
git add -N path/to/untracked/file # note: -N is short for --intent-to-add
git stash
这将失败,如下所示:
path/to/untracked-file: not added yet
fatal: git-write-tree: error building trees
Cannot save the current index state
那么,你能做什么?嗯,您必须真正添加文件,但是,您可以稍后使用git-rm-cached:
git add path/to/untracked-file
git stash save "don't forget to un-add path/to/untracked-file" # stash w/reminder
# do some other work
git stash list
# shows:
# stash@{0}: On master: don't forget to un-add path/to/untracked-file
git stash pop # or apply instead of pop, to keep the stash available
git rm --cached path/to/untracked-file
然后,您可以继续工作,与git添加之前的状态相同(即,使用名为path/to/untracked file的未跟踪文件;以及您可能需要跟踪文件的任何其他更改)。
此工作流的另一种可能是:
git ls-files -o > files-to-untrack
git add `cat files-to-untrack` # note: files-to-untrack will be listed, itself!
git stash
# do some work
git stash pop
git rm --cached `cat files-to-untrack`
rm files-to-untrack
[注意:正如@mancocapac的评论中所提到的,您可能希望在git-ls-files命令中添加--exclude standard(因此,git-ls-file-o--excludestandard)。]
…这也可以很容易地编写脚本——即使是别名也可以(以zsh语法显示;根据需要进行调整)[此外,我缩短了文件名,使其在屏幕上显示,而无需滚动此答案;请随意替换您选择的另一个文件名]:
alias stashall='git ls-files -o > .gftu; git add `cat .gftu`; git stash'
alias unstashall='git stash pop; git rm --cached `cat .gftu`; rm .gftu'
请注意,后者作为shell脚本或函数可能更好,以允许将参数提供给git stash,以防您不希望pop而是应用,和/或希望能够指定特定的stash,而不是只使用顶部的一个。也许是这样(而不是上面的第二个别名)[去掉空格以适应无需滚动;重新添加以增加易读性]:
function unstashall(){git stash "${@:-pop}";git rm --cached `cat .gftu`;rm .gftu}
注意:在此表单中,如果要提供存储标识符,则需要提供操作参数和标识符,例如unsshall apply stash@{1}或unsshall pop stash@}
当然,你会把你的.zshrc或同等文件放进去,以使其长期存在。
希望这个答案对某人有帮助,将所有事情都放在一个答案中。
您只需使用以下命令即可完成
git stash save --include-untracked
or
git stash save -u
有关git stash的更多信息,请访问此帖子(单击此处)
推荐文章
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别
- GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
- 撤消git平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?