将当前分支上的所有更改移动到Git中的一个新分支

时间:2020-12-18 23:44:45

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. 创建一个名为(say)的新分支“边缘”
  3. Move all the changed / untracked files on master to edge (such that master is unchanged from when I started the bug fix)
  4. 将主服务器上所有已更改/未跟踪的文件移动到边缘(这样自启动bug修复以来,主服务器将保持不变)
  5. Finish my work on edge, merge back into master
  6. 完成我的边缘工作,归并到大师

How can I do this?

我该怎么做呢?

3 个解决方案

#1


95  

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. 创建一个新的分支:git checkout -b edge
  3. Your files haven't changed. Just git add what needs to and commit as usual.
  4. 你的文件没有改变。只要git添加需要的内容并像往常一样提交即可。
  5. When you're done committing on edge, switch back to master with git checkout and git merge edge.
  6. 当您完成了在边缘上提交时,使用git checkout和git merge edge切换回master。

#2


88  

To add to JB's answer, if you have already started 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

#3


13  

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

#1


95  

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. 创建一个新的分支:git checkout -b edge
  3. Your files haven't changed. Just git add what needs to and commit as usual.
  4. 你的文件没有改变。只要git添加需要的内容并像往常一样提交即可。
  5. When you're done committing on edge, switch back to master with git checkout and git merge edge.
  6. 当您完成了在边缘上提交时,使用git checkout和git merge edge切换回master。

#2


88  

To add to JB's answer, if you have already started 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

#3


13  

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