Search code examples
gitgit-stashgit-index

Git stash removes added changes


During development, I routinely add working versions of files (but not commit them) to my git repo. I continue to work on those files, till they reach commitable stage, when I can commit them. So the repo looks like below

$ git status

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   testfile1
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   testfile1
#   modified:   testfile2

When I do a git stash, and then do a git stash pop, I get

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   testfile1
#   modified:   testfile2

Questions

  1. Why doesn't git stash them the way they were previously?
  2. How do I stash my changes in one go, so that on doing a stash pop, I get the older state, and not the newer one?

Currently, I manually do

git stash --keep-index
git stash
git stash pop
git add <stashed_files>
git stash pop

Problem with this is

  1. It takes 5 steps to stash & pop and I am looking for 2
  2. I sometimes may not remember there were 2 back to back stashes, which slightly complicates the task.

Edit - I would prefer a command line solution, since I work in a similar fashion on test servers.


Solution

  • use --index option.

    git stash
    git stash pop --index