16

这在我的工作流程中经常发生:我正在处理一个单独分支中的功能,并且在执行此操作时,我会遇到需要修复但来自框架或站点布局更高层的小问题。

我想切换回主开发分支并从那里的选择文件提交更改,然后返回到功能分支并重新设置基准,以便我可以继续在那里进行不相关的调整/错误修复。

我正在使用git stashgit stash pop执行此操作,但是我从一堆我修改但不需要提交到父分支的文件中遇到了很多冲突。

是否有另一种方法可以避免冲突或以某种方式保存当前状态,并且只将选择的工作树更改拉到另一个分支进行提交?(有点像 git-stash-cherry-pick ;-))

4

5 回答 5

15
  1. master在当前分支上提交您想要的更改。
  2. 存储剩余的更改
  3. 切换到master分支并使用git cherry-pick将更改移动到master
  4. 移回您的羽毛分支,并且rebase(可选)
  5. 取消隐藏原始功能更改
于 2010-10-07T00:34:36.717 回答
3

尝试使用选项切换回master分支。--merge它将尝试在两个分支之间进行三向合并。git 文档有一个很好的例子:

2. After working in the wrong branch, switching to the correct
   branch would be done using:

      $ git checkout mytopic

   However, your "wrong" branch and correct "mytopic" branch
   may differ in files that you have modified locally, in which 
   case the above checkout would fail like this:

      $ git checkout mytopic
      error: You have local changes to 'frotz'; not switching branches.

   You can give the -m flag to the command, which would try a
   three-way merge:

      $ git checkout -m mytopic
      Auto-merging frotz

   After this three-way merge, the local modifications are not
   registered in your index file, so git diff would show you what
   changes you made since the tip of the new branch.
于 2010-10-07T00:30:46.413 回答
3

我通常反过来做。我继续在我的功能分支中工作,直到我准备好在那里提交。一旦我加入,我会将属于分支的新提交的所有更改添加到索引中,但不添加属于 master 的更改。git add -p等人使这变得非常容易。一旦索引中的所有相关更改,我将提交到分支。所有剩余的脏更改都属于 master 并且一旦我切换到它就会很好地进行,这样我就可以在那里提交它。

于 2010-10-06T10:58:04.980 回答
2

在 MacOS 上,GitX使得执行 rafl 描述的那种选择性提交变得非常容易,所以如果这是你所处的环境,这是一个很好的方法。

在单独的提交中提交分支-y 更改和主-y 更改也是可能/实用的,然后用于git format-patch将分支中的提交导出为文件并将git am它们拉入主控。

这里的危险是,如果更改周围的文件差异太大,在这种情况下,将提交拉入主节点时可能会发生冲突。

于 2010-10-06T13:04:52.913 回答
1

创建一个临时分支怎么样?

就像是:

- oh crap need to do somethning else now
- git checkout -b ResumeLater
- git add .
- git commit
- git checkout ImportantStuff
- ....
- git checkout ResumeLater
于 2010-10-06T10:26:35.650 回答