這裏有一個小劇本我已經在過去拿出來做到這一點:
(注:我最初發布這個在https://stackoverflow.com/a/17137669/531021,但它似乎把這裏以及這些不完全重複的問題,所以我認爲它作爲在這兩種情況下可能的答案。)
#!/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 suceed
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
你的路徑上的某個地方,然後就可以調用它的混帳藏匿指數
# <hack hack hack>
git add <files that you want to stash>
git stash-index
現在藏匿包含一個新的條目,只包含你有變化分階段進行,並且您的工作樹仍然包含任何未分離的更改。
主要的疑難雜症是,你可能無法徹底移除該索引的改變,而不會造成衝突,例如如果工作樹包含取決於索引更改的更改。
在這種情況下,任何這樣的衝突將會留在通常的未合併衝突狀態,類似於一個摘櫻桃/合併之後。
例如
git init
echo blah >> "blah"
git add -A
git commit -m "blah"
echo "another blah" >> blah
git add -A
echo "yet another blah" >> blah
# now HEAD contains "blah", the index contains "blah\nanother blah"
# and the working tree contains "blah\nanother blah\nyetanother blah"
git stash-index
# A new stash is created containing "blah\nanother blah", and we are
# left with a merge conflict, which can be resolved to produce
# "blah\nyet another blah"
我認爲這只是'git的承諾'你想要 - 它需要你的索引並從它創建一個提交。 Kevin Ballard的答案解釋瞭如何在完成後明智地重寫歷史記錄...... – 2011-03-12 09:34:43
聽起來你想要類似於'git stash --keep-index'的東西,但是對於工作樹。即' - 工作樹'。我也是,它不存在。 – 2012-08-13 19:44:23
爲什麼不作弊? git stash --keep-index將當前不在索引中的所有內容都刪除。現在,git存儲只是上演的東西。混帳隱藏彈出第一個藏匿處,添加您的更改,提交。現在,git reset --hard清理工作樹,然後git存儲pop --index以重新獲得索引更改。 – doliver 2015-01-20 15:07:45