如何在我的分支上只隐藏多个已更改文件中的一个?
如何在我的分支上只隐藏多个已更改文件中的一个?
警告
正如评论中所指出的,这会将所有内容都放入存储中,无论是分阶段还是非分阶段。 -step-index只在保存完成后单独留下索引。以后弹出存储时,这可能会导致合并冲突。
这将隐藏您之前未添加的所有内容。只是 git add
你要保留的东西,然后运行它。
git stash --keep-index
例如,如果要将旧提交拆分为多个变更集,则可以使用此过程:
git rebase -i <last good commit>
edit
。git reset HEAD^
git add <files you want to keep in this change>
git stash --keep-index
git add
任何变化。git commit
git stash pop
git rebase --continue
你也可以使用 git stash save -p "my commit message"
。通过这种方式,您可以选择将哪些帅哥添加到藏匿处,也可以选择整个文件。
每个块都会提示您一些操作:
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基本上是关于管理所有存储库 内容 和索引(而不是一个或几个文件), git stash
交易,毫不奇怪, 与所有工作目录。
实际上,自Git 2.13(2017年第2季度)以来,您可以存储单个文件,包括:
git stash push [--] [<pathspec>...]
见“存储对特定文件的更改“更多。
原始答案(下面,2010年6月)是关于手动选择您想藏匿的内容。
Casebash 注释:
这个(
stash --patch
原始解决方案)很好,但我经常修改了很多文件,因此使用补丁很烦人
bukzor的 回答 (upvoted,2011年11月)提出了一个更实际的解决方案,基于
git add
+ git stash --keep-index
。
去看看并提出他的答案,这应该是官方的(而不是我的)。
关于该选项, chhh 在评论中指出了另一种工作流程:
你应该 ”
git reset --soft
“在这样的藏匿之后,让你的回归清晰:
为了达到原始状态 - 这是一个明确的暂存区域,并且只有一些选择的非阶段性修改,可以轻轻地重置索引以获取(不提交像你这样的东西 - bukzor - 确实)。
(原始答案2010年6月:手动藏匿)
然而, git stash save --patch
可以让你实现你所追求的部分存款:
同
--patch
,您可以交互式地从HEAD和工作树之间的差异中选择要存储的帅哥。
构建存储条目,使其索引状态与存储库的索引状态相同,并且其工作树仅包含您以交互方式选择的更改。然后,从您的工作树中回滚所选更改。
但是,这将保存完整索引(可能不是您想要的,因为它可能包括已编入索引的其他文件),以及部分工作树(可能看起来像您要隐藏的那个)。
git stash --patch --no-keep-index
可能更适合。
如果 --patch
不起作用,手动过程可能:
对于一个或多个文件,中间解决方案是:
git stash
git stash
#这次,只有你想要的文件被藏起来了git stash pop stash@{1}
#重新应用所有文件修改git checkout -- afile
#在任何本地修改之前,将文件重置为HEAD内容在相当繁琐的过程结束时,您将只有一个或几个文件被藏起来。
什么时候 git stash -p
(要么 git add -p
同 stash --keep-index
)太麻烦了,我发现它更容易使用 diff
, checkout
和 apply
:
仅“隐藏”特定文件/目录:
git diff path/to/dir > stashed.diff
git checkout path/to/dir
然后是
git apply stashed.diff
假设你有3个文件
a.rb
b.rb
c.rb
并且你想只存储b.rb和c.rb而不是a.rb
你可以做这样的事情
# commit the files temporarily you don't want to stash
git add a.rb
git commit -m "temp"
# then stash the other files
git stash save "stash message"
# then undo the previous temp commit
git reset --soft HEAD^
git reset
你完成了! HTH。
使用 git stash push
, 喜欢这个:
git stash push [--] [<pathspec>...]
例如:
git stash push -- my/file.sh
这是自2017年春季发布的Git 2.13开始提供的。
另一种方法:
# Save everything
git stash
# Re-apply everything, but keep the stash
git stash apply
git checkout <"files you don't want in your stash">
# Save only the things you wanted saved
git stash
# Re-apply the original state and drop it from your stash
git stash apply stash@{1}
git stash drop stash@{1}
git checkout <"files you put in your stash">
在我(再一次)来到这个页面并且不喜欢前两个答案之后我想出了这个(第一个答案就是没有回答这个问题而且我不太喜欢和 -p
互动模式)。
这个想法与@VonC建议使用存储库外部文件的想法相同,您可以保存您想要的更改,删除存储中不需要的更改,然后重新应用您移动的更改。但是,我使用git stash作为“某处”(结果,最后还有一个额外的步骤:删除你放在藏匿处的cahnges,因为你也将它们移开了)。
更新(2015年2月14日) - 我稍微重写了脚本,以便更好地处理冲突的情况,现在应该将其显示为未合并的冲突而不是.rej文件。
我经常发现做@ bukzor方法的倒数更直观。也就是说,要进行一些更改,然后只存储那些分阶段的更改。
不幸的是,git没有提供git stash --only-index或类似的东西,所以我掀起了一个脚本来做到这一点。
#!/bin/sh
# first, go to the root of the git repo
cd `git rev-parse --show-toplevel`
# create a commit with only the stuff in staging
INDEXTREE=`git write-tree`
INDEXCOMMIT=`echo "" | git commit-tree $INDEXTREE -p HEAD`
# create a child commit with the changes in the working tree
git add -A
WORKINGTREE=`git write-tree`
WORKINGCOMMIT=`echo "" | git commit-tree $WORKINGTREE -p $INDEXCOMMIT`
# get back to a clean state with no changes, staged or otherwise
git reset -q --hard
# Cherry-pick the index changes back to the index, and stash.
# This cherry-pick is guaranteed to succeed
git cherry-pick -n $INDEXCOMMIT
git stash
# Now cherry-pick the working tree changes. This cherry-pick may fail
# due to conflicts
git cherry-pick -n $WORKINGCOMMIT
CONFLICTS=`git ls-files -u`
if test -z "$CONFLICTS"; then
# If there are no conflicts, it's safe to reset, so that
# any previously unstaged changes remain unstaged
#
# However, if there are conflicts, then we don't want to reset the files
# and lose the merge/conflict info.
git reset -q
fi
您可以将上述脚本保存为 git-stash-index
路径上的某个地方,然后可以将其作为git stash-index调用
# <hack hack hack>
git add <files that you want to stash>
git stash-index
现在,存储包含一个新条目,该条目仅包含您已暂存的更改,并且您的工作树仍包含任何未暂存的更改。
在某些情况下,工作树更改可能取决于索引更改,因此当您隐藏索引更改时,工作树更改会发生冲突。在这种情况下,您将获得通常使用git merge / git mergetool / etc解决的未合并冲突。
由于在Git中创建分支是微不足道的,因此您可以创建一个临时分支并将单个文件检入其中。
以防你真正的意思 放弃更改 无论何时使用 git stash
(并且不要使用git stash暂时存储它),在这种情况下你可以使用
git checkout -- <file>
[注意]
那 git stash
只是分支和做事的快速而简单的替代方案。
将以下代码保存到文件中,例如,named stash
。用法是 stash <filename_regex>
。参数是文件完整路径的正则表达式。例如,要存储a / b / c.txt, stash a/b/c.txt
要么 stash .*/c.txt
等
$ chmod +x stash
$ stash .*.xml
$ stash xyz.xml
要复制到文件中的代码:
#! /usr/bin/expect --
log_user 0
set filename_regexp [lindex $argv 0]
spawn git stash -p
for {} 1 {} {
expect {
-re "diff --git a/($filename_regexp) " {
set filename $expect_out(1,string)
}
"diff --git a/" {
set filename ""
}
"Stash this hunk " {
if {$filename == ""} {
send "n\n"
} else {
send "a\n"
send_user "$filename\n"
}
}
"Stash deletion " {
send "n\n"
}
eof {
exit
}
}
}