你如何告诉 git 只存储索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5281663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you tell git to stash the index only?
提问by Malvineous
I have just used "git add -p" to add a bunch of changes to the index, and I just realised that I missed a change that should've gone into the previous commit.
我刚刚使用“git add -p”向索引添加了一系列更改,但我刚刚意识到我错过了本应进入上一次提交的更改。
I can't commit --amend now because I've added all these new changes to the index, and I don't want to use 'git reset' to remove them all from the index as it will take ages to add them all back in again.
我现在无法提交 --amend 因为我已将所有这些新更改添加到索引中,并且我不想使用“git reset”将它们全部从索引中删除,因为将它们全部添加需要很长时间再回来。
What I need is something like 'git stash' that will only stash the index - it should leave the working files alone. Then I can stash the index, add the missing change, commit it, then pop the stash and have my index back the way it was.
我需要的是像“git stash”这样的东西,它只会存储索引——它应该单独保留工作文件。然后我可以存储索引,添加丢失的更改,提交它,然后弹出存储并使我的索引恢复原状。
It doesn't look like 'git stash' is able to do this, but am I missing something? Thanks!
'git stash' 似乎无法做到这一点,但我是否遗漏了什么?谢谢!
采纳答案by Lily Ballard
Simplest way is to leave off that change right now, make your new commit, then create a second commit with just that change you want to use to amend and then use git rebase -i
to squash it with the original HEAD.
最简单的方法是立即停止该更改,进行新的提交,然后创建第二次提交,并使用您要用于修改的更改,然后使用git rebase -i
原始 HEAD 压缩它。
An alternative would be to make your commit, tag it, roll back with git reset HEAD^
, add that one change and amend HEAD, then cherry-pick your tagged commit.
另一种方法是进行提交,标记它,使用 回滚git reset HEAD^
,添加一个更改并修改 HEAD,然后挑选标记的提交。
回答by Leif Gruenwoldt
The closest thing I've found is git stash --patch
. It walks you through each of the changes to working tree and index letting you choose what to stash.
我发现的最接近的东西是git stash --patch
. 它会引导您完成对工作树和索引的每个更改,让您选择要存储的内容。
http://www.kernel.org/pub/software/scm/git/docs/git-stash.html
http://www.kernel.org/pub/software/scm/git/docs/git-stash.html
回答by doliver
Why not cheat?
为什么不作弊?
git stash --keep-index
to get everything out of there that's not in the index currently. Then,
将当前不在索引中的所有内容都删除。然后,
git stash
to get a stash with just the stuff that's staged.
将上演的东西藏起来。
git stash pop
the first stash, add your changes. Then,
第一个藏匿处,添加您的更改。然后,
git commit --amend ...
git reset --hard
to clean up the working tree and then
清理工作树,然后
git stash pop --index
to get your index changes back.
恢复您的索引更改。
回答by LeGEC
git stash
does actually create a commit with the index content, and then adds a commit with the content of all tracked files on top of it
git stash
确实创建了一个带有索引内容的提交,然后在它上面添加了一个带有所有跟踪文件内容的提交
To view this : create a stash, then run
要查看这个:创建一个 stash,然后运行
git log --oneline --graph stash@{0}
So technically, when you stash, you can get back your index through stash@{0}^2
:
所以从技术上讲,当你藏起来时,你可以通过stash@{0}^2
以下方式取回你的索引:
$ git show --name-only stash@{0}^2
$ git checkout stash@{0}^2 -- .
You can also stash, and then get the content of the tracked-but-not-addedfiles :
您还可以存储,然后获取跟踪但未添加的文件的内容:
# get the diff between what was indexed and the full stashed content :
$ git diff -p stash@{0}^2 stash@{0} > diff.patch
# apply this diff :
$ git apply diff.patch
回答by user1338062
Commit your index, create a fixup commit, and rebase using autosquash:
提交您的索引,创建一个修复提交,并使用 autosquash 变基:
git commit
git add -p # add the change forgotten from HEAD^
git commit --fixup HEAD^ # commits with "fixup! <commit message of HEAD^>"
git rebase --autosquash -i HEAD~3
回答by imalison
回答by Oli
This seems to work. I haven't tested it under all situations to make sure it's robust:
这似乎有效。我尚未在所有情况下对其进行测试以确保其稳健:
git commit -m _stash && git stash && git reset HEAD^ && git stash save && git stash pop stash@{1}
But, it effectively commits the index temporarily, stashes the working directory, reverts the commit to get the index back, saves it as another stash, then restores the original working directory again from the stash.
但是,它有效地临时提交索引,存储工作目录,恢复提交以取回索引,将其另存为另一个存储,然后再次从存储中恢复原始工作目录。
This is simplified by adding this as a git alias:
通过将其添加为 git 别名来简化此操作:
[alias]
istash = "!f() { git commit -m _stash && git stash && git reset HEAD^ && git stash save && git stash pop stash@{1}; }; f"
This allows me to use it like:
这使我可以像这样使用它:
git istash [optional stash name]
回答by driusan
I had to do this, and ended up using some of the git stash options intended for scripting. Git stash create let's you create (but not apply) a stash object (which includes the index). Git stash store adds it to the git stash stack without doing the git reset --hard that git stash save does implicitly. This, effectively, let's you stash an index by adding a stash object to the stack and resetting the index manually:
我不得不这样做,并最终使用了一些用于编写脚本的 git stash 选项。Git stash create 让你创建(但不应用)一个 stash 对象(包括索引)。Git stash store 将它添加到 git stash 堆栈而不执行 git reset --hard git stash save 隐式执行的操作。这有效地让您通过将存储对象添加到堆栈并手动重置索引来存储索引:
# This will add an entry to your git stash stack, but *not* modify anything
git stash store -m "Stashed index" $(git stash create)
# This will reset the index, without touching the workspace.
git reset --mixed
You've now, effectively, stashed your index and can do a git stash pop --index when you're done.
您现在已经有效地隐藏了索引,并且可以在完成后执行 git stash pop --index 。
One caveat is that the stash object created includes both the files changed in the workspace and the index, even though you're only concerned about the index, so there's a possibility of conflicts when you try and pop from the stack even though you used the --index flag (which means "also apply the index from the stash", not "only apply the index from the stash"). In that case, you can do a "git reset --hard" before popping (because the changes that you're resetting happen to also be the same as the changes that you're popping or applying immediately after, it's not as dangerous as it seems.)
一个警告是,创建的 stash 对象包括工作区中更改的文件和索引,即使您只关心索引,因此即使您使用--index 标志(这意味着“还应用存储中的索引”,而不是“仅应用存储中的索引”)。在这种情况下,您可以在弹出之前执行“git reset --hard”(因为您正在重置的更改恰好也与您之后立即弹出或应用的更改相同,它不像它似乎。)
回答by user9058026
git stash -k
git stash
git stash apply stash@{1}
git stash drop stash@{1}
回答by mmm
If you add files ready to commit to the index, but want to stageonly them, you can use the function below.
如果您添加准备提交到索引的文件,但只想暂存它们,您可以使用下面的功能。
>> momomo.com.git.stash.added
or
或者
>> momomo.com.git.stash.added "name of the stash"
Bash function:
重击功能:
momomo.com.git.stash.added() {
local name="";
if [[ "${name}" == "" ]]; then
name="$(date)"
fi
# This will stash everything, but let the added ones remain
# stash@{1}
git stash --keep-index
# This will stash only the remaining ones
# @stash@{0}
git stash save "${name}"
# Restore everything by applying the first push stash which is now at index 1
git stash apply stash@{1}
# Then drop it
git stash drop stash@{1}
# At the top of the stash should now be only the originally indexed files
}
Note that you will need to re add things to the index now.
请注意,您现在需要将内容重新添加到索引中。