将我在当前分支上的所有更改移到 Git 中的新分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1398329/
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-10 07:00:33  来源:igfitidea点击:

Take all my changes on the current branch and move them to a new branch in Git

gitbranch

提问by Tom Lehman

I started work on what I thought would be a minor bug fix on my master branch. However, it has spiraled out of control to the point where I wish I had created a separate branch to do the development in the first place.

我开始在我的主分支上修复我认为的一个小错误。然而,它已经失控到我希望我首先创建一个单独的分支来进行开发的地步。

So right now what I'd like to do is:

所以现在我想做的是:

  1. Create a new branch called (say) "edge"
  2. Move all the changed / untracked files on master to edge (such that master is unchanged from when I started the bug fix)
  3. Finish my work on edge, merge back into master
  1. 创建一个名为(比如说)“edge”的新分支
  2. 将 master 上所有更改/未跟踪的文件移动到边缘(这样 master 从我开始修复错误时就没有改变)
  3. 在边缘完成我的工作,合并回主人

How can I do this?

我怎样才能做到这一点?

采纳答案by JB.

If you haven't been committing anything yet, you're already in the right position.

如果您还没有提交任何内容,那么您已经处于正确的位置。

  1. Create a new branch: git checkout -b edge
  2. Your files haven't changed. Just git addwhat needs to and commit as usual.
  3. When you're done committing on edge, switch back to masterwith git checkoutand git merge edge.
  1. 创建一个新分支: git checkout -b edge
  2. 你的文件没有改变。只是git add像往常一样需要和提交。
  3. 完成提交后edge,切换回masterwithgit checkoutgit merge edge

回答by VonC

To add to JB's answer, if you have alreadystarted to make a few commits on master for what ended up as being a "edge" effort, you could:

要添加到 JB 的答案中,如果您已经开始在 master 上进行一些提交以最终成为“边缘”努力,您可以:

git stash
git checkout -b edge master
git branch -f master SHA1_before_your_commits
git stash apply

回答by Jesse P

If you are trying to move the work from master to a branch that already exists, but is behind master, git won't let you switch to the other branch. In this case, do this:

如果您尝试将工作从 master 移动到已经存在但位于 master 之后的分支,则 git 不会让您切换到另一个分支。在这种情况下,请执行以下操作:

git stash
git checkout oldBranch
git merge master
git checkout master
git stash apply
git checkout oldBranch