2

https://stackoverflow.com/a/48156644/156458 says

So you've git add-ed your changes, so they are currently staged. First you want to unstage them with git reset HEAD . assuming you are the top level in your directory. The files should all be (most likely) red now, indicating there are unstaged changes.

Now you can git stash those changes while you create a new branch. git checkout <<master branch name>> then git checkout -b <<new topic branch name>>.

You've got a clean copy of master now, so let's get those stashed changes with git stash pop. Now you're back to where you started, with all your already committed changes in the trunk and your new changes ready for commit.

Why "you want to unstage them with git reset HEAD"?

What does git stash push? Does git stash stash working directory only , or both working directory and index?

halfer
  • 19,824
  • 17
  • 99
  • 186
Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

1

Although there might be some other context we don't know of here, on the point of unstaging before stashing : git stash does stash the index as well as the working tree.

From git-stash man page :

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory

(emphasis mine)

I tried it on a spare repo to confirm it and yes it does :

$ git stash
Saved working directory and index state WIP on master: 4dea0a2 Fonction ...

I can't (and don't want to) claim the given advice was bad because again I don't know the underlying context, but either I'm missing something or it's an overcautious and thus somewhat useless/harmless step in the process.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61