如何在我的分支中只存储多个已更改文件中的一个?
当前回答
git stash push -p -m "my commit message"
-p让我们选择应该隐藏的大块;也可以选择整个文件。
系统将提示您对每个大块执行一些操作:
y - stash this hunk
n - do not stash this hunk
q - quit; do not stash this hunk or any of the remaining ones
a - stash this hunk and all later hunks in the file
d - do not stash this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
其他回答
自Git 2.13(2017年第二季度)以来,您可以使用Git stash推送来存储单个文件:
git stash push [-m <message>] [--] [<pathspec>...]
当路径规范被赋予“git stash push”时,新的stash只记录与路径规范匹配的文件的修改状态有关详细信息,请参阅“Stash changes to specific files”。
简化示例:
git stash push path/to/file
此功能的测试用例显示了更多选项:
test_expect_success 'stash with multiple pathspec arguments' '
>foo &&
>bar &&
>extra &&
git add foo bar extra &&
git stash push -- foo bar &&
test_path_is_missing bar &&
test_path_is_missing foo &&
test_path_is_file extra &&
git stash pop &&
test_path_is_file foo &&
test_path_is_file bar &&
test_path_is_file extra
最初的答案(下图,2010年6月)是关于手动选择你想要隐藏的东西。
Casebash注释:
这(隐藏的补丁原始解决方案)很好,但我经常修改很多文件,所以使用补丁很烦人
bukzor的回答(2011年11月投票通过)提出了一个更实际的解决方案,基于git add+git stash--保留索引。去看看并投票给他的答案,这应该是官方的答案(而不是我的答案)。
关于该选项,chhh在评论中指出了另一种工作流:
你应该在这样一个隐藏之后“git-reset-soft”,以获得清晰的分段:为了达到原始状态-这是一个清晰的暂存区,只有一些选择的未暂存修改,可以轻轻地重置索引以获取(而不需要像bukzor那样提交任何操作)。
(原答案2010年6月:手动储存)
然而,git-stash-save补丁可以让你实现你想要的部分存储:
使用--patch,您可以从HEAD和要隐藏的工作树之间的差异中交互选择大块。存储项的构造使其索引状态与存储库的索引状态相同,并且其工作树仅包含您以交互方式选择的更改。然后,所选更改将从工作树回滚。
然而,这将保存完整的索引(这可能不是您想要的,因为它可能包含其他已索引的文件)和部分工作树(看起来可能像您想要隐藏的文件)。
git stash --patch --no-keep-index
可能更合适。
如果--patch不起作用,手动过程可能会:
对于一个或多个文件,中间解决方案是:
将它们复制到Git存储库之外(实际上,eleotlecram提出了一个有趣的替代方案)暂存把它们复制回来git stash#这次,只存储您想要的文件git stash pop stash@{1}#重新应用所有文件修改gitcheckout--afile#在任何本地修改之前将文件重置为HEAD内容
在这个相当繁琐的过程结束时,您将只存储一个或多个文件。
由于在Git中创建分支很简单,您可以创建一个临时分支并将各个文件检入其中。
在这种情况下,我使用gitadd-p(交互式)、gitcommit-m blah,然后在必要时存储剩余的内容。
我没有找到我需要的答案,这很简单:
git add -A
git reset HEAD fileThatYouWantToStash
git commit -m "committing all but one file"
git stash
这只存储了一个文件。
git stash push -p -m "my commit message"
-p让我们选择应该隐藏的大块;也可以选择整个文件。
系统将提示您对每个大块执行一些操作:
y - stash this hunk
n - do not stash this hunk
q - quit; do not stash this hunk or any of the remaining ones
a - stash this hunk and all later hunks in the file
d - do not stash this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
推荐文章
- GitHub -未能连接到GitHub 443 windows/连接到GitHub失败-无错误
- 如何使Eclipse/EGit在更新后识别现有的存储库信息?
- 撤销意外git隐藏pop
- 我怎么能让詹金斯CI与Git触发器推到主人?
- Git:哪个是分支的默认配置远程?
- 如何从拉请求中删除提交
- 如何用分发文件发布npm包?
- 跟踪在GitHub上创建的一个新的远程分支
- 没有空格的合并会产生冲突
- 如何使用refspec将Git标记推到分支?
- 如何使用Git向远程存储库进行初始推送?
- 将文件的当前工作副本与另一个分支提交的副本区别对待
- 配置Git接受特定https远程的特定自签名服务器证书
- 如何将一个特定的提交从一个分支合并到Git中的另一个分支?
- 通过SSH配置Git登录一次