我在一个分支上做了一些改变。然后,我将更改隐藏到分支,然后通过以下方式创建了一个新分支:
git stash save "message"
git checkout -b newbranch oldbranch
现在我想回到我藏匿时的版本,我该怎么做?我也没有对新分支进行任何更改,所以如果我丢失了新分支的任何信息,我就不在乎了,我将删除它。
答案 0 :(得分:2)
这取决于你的意思
回到我藏匿时的版本
如果要检索隐藏的更改,可以使用git stash pop
或git stash apply
。不同之处在于pop
将从存储中移除更改,而apply
会将更改保留在存储中(即可以再次应用它们)。
如果您想切换回oldbranch
,可以使用git checkout oldbranch
。
以下是一个示例工作流程:
# 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