如何在我的分支中只存储多个已更改文件中的一个?


当前回答

如果不想指定包含隐藏更改的消息,请在双破折号后传递文件名。

$ git stash -- filename.ext

如果它是一个未跟踪的/新的文件,则必须先将其暂存。

此方法适用于git2.13版本+

其他回答

类似的情况。做了承诺,意识到这不好。

git commit -a -m "message"
git log -p

根据这些回答,这对我有帮助。

# revert to previous state, keeping the files changed
git reset HEAD~
#make sure it's ok
git diff
git status
#revert the file we don't want to be within the commit
git checkout specs/nagios/nagios.spec
#make sure it's ok
git status
git diff
#now go ahead with commit
git commit -a -m "same|new message"
#eventually push tu remote
git push
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之前的git。对于2.13及以上的数字,请查看下面的另一个答案。


警告

正如评论中所指出的那样,这将所有的东西都放进了储藏室,无论是暂存的还是未暂存的。--keep索引只是在存储完成后保留索引。这可能会在以后弹出存储时导致合并冲突。


这将隐藏您以前未添加的所有内容。只需git添加要保留的内容,然后运行它。

git stash --keep-index

例如,如果要将旧提交拆分为多个变更集,可以使用以下过程:

gitrebase-i<最后一次提交>将某些更改标记为编辑。git重置HEAD^git add<您要在此更改中保留的文件>git stash—保留索引必要时修理一下。不要忘记git添加任何更改。git提交吉特藏弹根据需要,从第5步开始重复。git rebase—继续

要隐藏单个文件,请使用git stash--patch[file]。

这将提示:Stash This hunk[y,n,q,a,d,j,j,g,/,e,?]?。只需键入一个(将此大块和所有后续大块存储在文件中),就可以了。