git 将 stash 应用到不同的分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37417792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:11:59  来源:igfitidea点击:

Apply stash to different branch

gitgit-stash

提问by MeesterPatat

I was accidentally working on the wrong branch. Now I want to transfer all my changes to the correct branch.

我不小心在错误的分支上工作。现在我想将所有更改转移到正确的分支。

If I stash the changes and apply them on the correct branch, will it only add the uncommitted changes to the correct branch or every change/commit from the wrong branch that doesn't exist on the correct branch?

如果我存储更改并将它们应用到正确的分支上,它是否只会将未提交的更改添加到正确的分支或来自正确分支上不存在的错误分支的每个更改/提交?

For example

例如

Wrong branch has:

错误的分支有:

  • Commit a

  • Uncommitted Changes b

  • 提交一个

  • 未提交的更改 b

Correct branch has

正确的分支有

  • Commit c
  • 提交 c

If I do git stash on the wrong branch and git apply stash in the correct branch, will it transfer commit a to the correct branch?

如果我在错误的分支上执行 git stash 并在正确的分支中执行 git apply stash,它会将提交转移到正确的分支吗?

回答by miguel_ibero

I would do one stash, then reset (mixed so you don't lose the changes) the a commit, stash that, then change to the correct branch and pop both stashes.

我会做一个存储,然后重置(混合所以你不会丢失更改)提交,存储,然后更改为正确的分支并弹出两个存储。

git stash save "b"
git reset HEAD~
git stash save "a"
git checkout correct-branch
git stash pop
git commit -m "a"
git stash pop

回答by miguelfg

If your branch doesn't exist yet:

如果您的分支尚不存在:

  • git stash branch "new_branch"
  • git stash 分支“new_branch”

if it does:

如果是这样:

  • git stash branch "temp_new_branch"
  • git add "your changes"
  • git commit
  • git checkout "your desired branch to apply stash"
  • git merge "temp_new_branch"
  • git push
  • git branch -d "temp_new_branch"
  • git stash 分支“temp_new_branch”
  • git add “你的改动”
  • 提交
  • git checkout “你想要的分支来应用 stash”
  • git合并“temp_new_branch”
  • git branch -d "temp_new_branch"

回答by dYale

Workaround

解决方法

  1. Make a commit with those desired changes.
  2. Checkout to the branch you want those changes to be on.
  3. From that branch git cherry-pick 23h123kjb(<-- replace this hash with the one found in a git logspecific to the commit you want to bring in)
  4. Profit!
  1. 使用这些所需的更改进行提交。
  2. 结帐到您希望进行这些更改的分支。
  3. 从该分支git cherry-pick 23h123kjb(<-- 将此哈希替换git log为您要引入的特定提交中找到的哈希)
  4. 利润!

回答by Bugfinger

No it won't. Commits are not put in the Stash. I also sometimes just switch branches with my changes uncommitted and unstashed and it also works (not sure if in every case, though).

不,不会。提交不会放入 Stash。有时我也只是在更改未提交和未隐藏的情况下切换分支,它也可以工作(但不确定是否在每种情况下)。

回答by Serve Laurijssen

What you can also do:

您还可以做什么:

  • Create a patch on the 'wrong' branch.
  • Discard the changes
  • Switch to the correct branch
  • Apply the patch on that branch
  • 在“错误”分支上创建补丁。
  • 放弃更改
  • 切换到正确的分支
  • 在该分支上应用补丁