我在分支上做了一些更改。然后,我将更改隐藏到分支中,随后通过以下方式创建了一个新分支:
git stash save "message"
git checkout -b newbranch oldbranch
现在我想回到我藏匿时的版本,我该怎么做?我也没有对 newbranch 进行任何更改,所以我不在乎我是否丢失了 newbranch 中的任何信息,我只会删除它。
这取决于你的意思
回到我藏匿时所在的版本
如果要检索存储的更改,可以使用 usinggit 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