本文翻译自:git stash changes apply to new branch?
I was working on master branch, made some changes and then stashed them. 我正在处理master分支,做了一些修改然后把它们藏起来了。 Now, my master is at HEAD. 现在,我的主人在HEAD。
But now, I want to retrieve these changes but to a new branch which branches from the HEAD version of the master branch. 但是现在,我想要检索这些更改,但要检索从主分支的HEAD版本分支的新分支。
How do i do this ? 我该怎么做呢 ?
#1楼
参考:https://stackoom.com/question/T3X9/git-stash更改适用于新分支
#2楼
Since you've already stashed your changes, all you need is this one-liner: 既然你已经隐藏了你的变化,你只需要这个单行:
-
git stash branch <branchname> [<stash>]
From the docs ( https://www.kernel.org/pub/software/scm/git/docs/git-stash.html ): 从文档( https://www.kernel.org/pub/software/scm/git/docs/git-stash.html ):
Creates and checks out a new branch named < branchname > starting from the commit at which the < stash > was originally created, applies the changes recorded in < stash > to the new working tree and index. 创建并检查了从开始的承诺,在该< 藏匿 >最初创建,适用记录在< 藏匿 >更改到新的工作树和索引命名为一个新的分支< 分支名称 >。 If that succeeds, and < stash > is a reference of the form stash@{< revision >}, it then drops the < stash >. 如果成功,并且< stash >是stash @ {< revision >}形式的引用,则它将删除< stash >。 When no < stash > is given, applies the latest one. 如果没有给出< stash >,则应用最新的。
This is useful if the branch on which you ran git stash save has changed enough that git stash apply fails due to conflicts. 如果运行git stash save的分支已经发生了足够的变化,git stash apply因冲突而失败,那么这很有用。 Since the stash is applied on top of the commit that was HEAD at the time git stash was run, it restores the originally stashed state with no conflicts. 由于存储是在运行git stash时的HEAD提交之上应用的,因此它会恢复原始存储状态而不会发生冲突。
#3楼
If you have some changes on your workspace and you want to stash them into a new branch use this command: 如果您的工作区有一些更改,并且您希望将它们存储到新分支中,请使用以下命令:
git stash branch branchName
It will make: 它会:
- a new branch 一个新的分支
- move changes to this branch 将更改移至此分支
- and remove latest stash (Like: git stash pop) 并删除最新的存储(如:git stash pop)
#4楼
Is the standard procedure not working? 标准程序不起作用吗?
- make changes 做出改变
-
git stash save
-
git branch xxx HEAD
-
git checkout xxx
-
git stash pop
Shorter: 短:
- make changes 做出改变
-
git stash
-
git checkout -b xxx
-
git stash pop