简体   繁体   中英

How to git stash only staged files?

During interactive rebase, I often find I want to make some changes appear BEFORE the current changeset, eg if I have:

A->BCD->E

I want to move to:

A->D->BC->E

So I do an interactive rebase, and edit the relevant changeset. So I'd like to be able to:

  1. reset --soft
  2. commit BCD
  3. unstage D
  4. stash the staged changes
  5. commit D
  6. pop the stash
  7. commit

Is this possible, or is there a simpler way to achieve this?

I have asked the question on the official git mailing list, but didn't got a real answer. http://git.661346.n2.nabble.com/Proposal-for-git-stash-add-staged-option-td7629171.html

Here is what I use now :

It makes it possible to do git cstash "optional message" which will stash all your staged files into a stash. It handles both dirty and clean states.

#/bin/sh
#
function evil_git_dirty {
  [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] && echo "dirty" || echo "clean"
}

function create_stash {
    # And convert into a stash
    if [ "$1" == "" ]
    then
        # Unnamed stash
        git stash
    else
        # Named stash
        git stash save "$1"
    fi
}

IS_DIRTY=$(evil_git_dirty)

echo "$IS_DIRTY"

if [ "$IS_DIRTY" == "clean" ] 
then
    create_stash "$1"
    exit 0
fi

# Put staged files in a tempory commit
git commit -m 'temporary, will be stashed soon' --no-verify
# Put everything else in a temporary stash
git add .
git stash
# Remove commit
git reset HEAD^1

git add .

create_stash "$1"
# Pop the temporary stash
git stash pop stash@{1}
git reset .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM