0

I started working on a feature in the wrong branch (let's call it branch 'A'). I had both modified and untracked files.

I wanted to get the work I've done and take it to a new branch ('B'). This what I did thinking I would have everything "as it is" in the new branch 'B':

git stash --all  
git checkout 'B'
git stash pop 

However, now only the untracked files are there, but not the ones modified.

What am I missing?

borjagvo
  • 1,802
  • 2
  • 20
  • 35

1 Answers1

1

What happened

git stash pop should have reapplied all your changes to your working tree. Something else which you are not telling us about might have happened.

Note that, in order to get "everything as it was" back, including the state of the index if you have already staged some changes, you have to run:

git stash apply --index
git stash drop

The --index flag, not available with git stash pop, restores the index.

If you did not have staged changes or if you did not care about restoring the index, what you did is correct. So there must be something else.

Alternative workflow in your situation

Something else that you could have done, once you realized that you were making changes on the wrong branch is:

  1. Move HEAD to branch B without touching at the working tree or the index:
git reset --soft B

So now HEAD points to branch B but your working tree and index haven't changed and thus still contain the changes you made while you were on the wrong branch.

  1. Finish your changes, stage them, and commit them

For instance:

git add .
git commit -m "..."
prosoitos
  • 6,679
  • 5
  • 27
  • 41
  • Thanks.That's useful. However, how would you do if B branch was still not there and you had to create it? Will "git reset --soft B" still work (create branch and move HEAD at the same time)? – borjagvo Oct 22 '20 at 16:47
  • No, `git reset` will not create a new branch. If B doesn't exist yet, simply create it with `git branch B` just before running `git reset --soft B`. – prosoitos Oct 22 '20 at 19:04
  • Note that the workflow in your question doesn't work either if B doesn't exist yet. You'd have to run `git checkout -b B` – prosoitos Oct 22 '20 at 19:06