Reputation: 2896
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.
Upvotes: 1
Views: 1772
Reputation: 6877
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
Upvotes: 6