1

I was on a branch and made some changes. I then stashed the changes to the branch and subsequently made a new branch via:

git stash save "message"

git checkout -b newbranch oldbranch

Now I want to go back to the version I was in when I stashed, how do I do that? I also didn't make any changes in the newbranch so I don't care if I lose any information from the newbranch, I will just be deleting it.

melpomene
  • 84,125
  • 8
  • 85
  • 148
conv3d
  • 2,668
  • 6
  • 25
  • 45

1 Answers1

6

It depends on what you mean by

go back to the version I was in when I stashed

If you want to retrieve the stashed changes, you can use either use git stash pop or git stash apply. The difference being that pop will remove the changes form the stash and apply will leave the changes in stash (i.e. so they can be applied again).

If you want to switch back to oldbranch, you can use git checkout oldbranch.


Here's an example workflow:

# start a fresh repository
$ git init
Initialized empty Git repository in /home/chuckx/code/stackoverflow/git-stash/.git/

# start a fresh branch
$ git checkout -b branch1
Switched to a new branch 'branch1'

# populate a file with content and commit it
$ echo branch1 content > file.txt
$ git add file.txt
$ git commit -m "branch1 content"
[branch1 (root-commit) dadf402] branch1 content
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt

# make a post-commit change to the file
$ echo stashed content >> file.txt
$ git status
On branch branch1
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:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")

# stash the post-commit change
$ git stash save
Saved working directory and index state WIP on branch1: dadf402 branch1 content

# verify that we're back to a clean working tree with no changes
$ git status
On branch branch1
nothing to commit, working tree clean

# start a new branch
$ git checkout -b branch2
Switched to a new branch 'branch2'

# make a change
$ echo branch2 content >> file.txt
$ git status
On branch branch2
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:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ cat file.txt
branch1 content
branch2 content


# decide that we do not want to commit any changes to the new branch

# switch back to the original branch
$ git checkout branch1
M       file.txt
Switched to branch 'branch1'

# notice that our unstaged changes carry over after the branch switch
$ git status
On branch branch1
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:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ cat file.txt
branch1 content
branch2 content


# perform a checkout of all files to fetch files as we left them in
# in the original branch
$ git checkout .
$ git status
On branch branch1
nothing to commit, working tree clean
$ cat file.txt
branch1 content

# retrieve the stashed changes and apply them over the fresh orignal
# branch checkout
$ git stash pop
On branch branch1
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:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (9a326f0ff35f65313da479c742b624870807f550)
$ cat file.txt
branch1 content
stashed content
chuckx
  • 6,484
  • 1
  • 22
  • 23
  • How do I read the: <<<<<<< Updated upstream ======= >>>>>>> Stashed changes Is this whole section from the stashed changes or from the branch? – conv3d Jun 10 '18 at 19:26
  • @jchaykow I'm not sure what command you've run and what you're looking at. It may be useful to update your question with more context around what you're attempting. – chuckx Jun 10 '18 at 19:48
  • 2
    @jchaykow Those are [conflict markers](https://stackoverflow.com/a/10657351/7976758) after `git stash apply/pop`. You need to resolve the conflicts. – phd Jun 11 '18 at 10:02
  • FYI, added an example workflow. My guess (based on my understanding of what you've described) is that you did not flush out your unstaged changes after switching back to `oldbranch` (see the `git checkout .` command in the example workflow). – chuckx Jun 11 '18 at 16:35
  • Did this answer your question? – chuckx Jun 15 '18 at 07:01