获取当前分支上的所有更改,并将它们移动到 Git 中的一个新分支

我开始着手修复我的主分支上的一个小错误。然而,它已经失去了控制,以至于我希望当初创建一个单独的分支来进行开发。

所以现在我想做的是:

  1. 创建一个名为(比如)“ edge”的新分支
  2. 将 master 上所有已更改/未跟踪的文件移动到 edge (这样 master 与我开始修复 bug 时没有变化)
  3. 完成我的边缘工作,融合回主人

我怎么能这么做?

32407 次浏览

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 add what needs to and commit as usual.
  3. When you're done committing on edge, switch back to master with git checkout and git merge edge.

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:

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

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:

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