7

I made few changes in local master branch. The local changes were not staged (added). In order to update the master branch, I did the following:

  1. statshed my local changes.
  2. git pull
  3. git stash apply

I see that after this the local changes get staged (added) ? Shouldn't they be kept unstaged as they were before.

kostix
  • 51,517
  • 14
  • 93
  • 176
Karun
  • 447
  • 5
  • 17
  • I think you are asking the same as this question: https://stackoverflow.com/questions/1237021/why-does-git-stash-apply-stage-my-changes – Tor Klingberg Feb 21 '18 at 16:05

2 Answers2

5

git stash is essentially the same as git commit. It creates a fully fledged commit, but doesnt add it to the history. Instead, it adds the commit to the stash.

Therefore, git stash has to make a choice: Either commit (stash) all the uncommited changes, or only commit (stash) the ones that are added to the index. It can't do both. That would take two commits instead of one.

AFAIK, git stash takes all uncommited changes.

functionpointer
  • 1,411
  • 1
  • 13
  • 18
  • In that case, can I unstage the changes staged by "git stash apply"? – Karun Sep 11 '13 at 13:07
  • Yes, of course. `git stash apply` just stages the changes to help you. If you dont want them staged, unstage them. – functionpointer Sep 11 '13 at 13:26
  • 1
    Aren't you contradicting the documentation? The entry for `git stash pop` mentions the `--index` option, and tells about it that `If the --index option is used, then tries to reinstate not only the working tree’s changes, but also the index’s ones. However, this can fail, when you have conflicts (which are stored in the index, where you therefore can no longer apply the changes as they were originally).` In my eyes that means that unless `--index` is specified, the stashed changes are applied to the work tree only, not touching the index. – kostix Sep 11 '13 at 13:43
  • @Karun, what program are you using to interact with your Git repository? Vanilla Git client or something else? – kostix Sep 11 '13 at 13:43
  • @kostix I wasn't aware of that. Obviously, my answer is not entirely corrent. `git stash` saves also the state of the index. Not sure why the changes are added to the index in karun's case, because i interpret the `--index` option the same way as you. – functionpointer Sep 11 '13 at 13:58
  • I suspect @Karun employs some program (or library) other than Git itself to work with the repository. Such a piece of software is only required to obey the format of the repository object- and configuration storage, and basically follow the semantics (obviously, say, a menu option named "Stash" should not pefrorm fetching), but otherwise it has a lot of freedom when it comes to details -- for instance, I'd not be surprised to learn that applying a stash entry in a Git front-end Foo works like `git stash apply --index`. – kostix Sep 11 '13 at 16:04
  • 2
    FYI, `git stash` actually *does* make two commits (sometimes three!). When `git stash` finishes, `refs/stash` is a two-or-three-parent commit. The "main" commit, `refs/stash` itself, contains the work tree state, and `refs/stash^2` contains the saved index state. The third commit, `refs/stash^3`, exists only if you used `-u` or `-a` and contains the untracked (and with `-a`, ignored) files. (And `git rev-parse refs/stash^` == `git rev-parse HEAD`.) – torek Sep 11 '13 at 19:10
  • @kostix I am not using any GIT client. I am working with GIT directly. – Karun Sep 12 '13 at 10:20
  • @Karun, very strange indeed. Can we know the output of `git --version` then? And if you run `git help stash`, does its entry on `git stash pop` menton the behaviour of the `--index` option I cited in my first comment? I suspect the Git's behaviour might have changed. – kostix Sep 12 '13 at 13:47
2

git stash also adds changes to your local system but won't add them to the tree. You can always see all the stashed stuff by command git stash list. You can also add your stash content later after changes in the main content like after pull cammand etc. I would suggest you to follow the original docs of the git to make it clear. Here is the http://git-scm.com/book/en/Git-Tools-Stashing

Jatin Malwal
  • 5,133
  • 2
  • 23
  • 26