Вот небольшой скрипт, который я придумал в прошлом, чтобы сделать именно это:
(Примечание: я первоначально разместил это на 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
где-нибудь на вашем пути, а затем вызвать егокак 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"