将选择的更改提交到另一个分支,然后恢复当前分支的工作?
这种情况经常发生在我的工作流程中:我正在一个单独的分支中开发一个功能,在执行此操作时,我会遇到需要修复但来自框架或站点布局较高层的小问题。
我想切换回主 develop 分支并从那里选择的文件提交更改,然后返回到 feature 分支,并重新设置基准,以便我可以继续那里不相关的调整/错误修复。
我正在使用 git stash 和 git stash pop 来执行此操作,但我从我修改但不需要的一堆文件中遇到了很多冲突无论如何都要致力于父分支。
是否有另一种方法可以避免冲突或以某种方式保存当前状态,并且仅将选择的工作树更改拉到另一个分支以进行提交? (有点像 git-stash-cherry-pick ;-))
This happens often in my work flow: I'm working on a feature in a separate branch, and while doing this I'll come across small things that need fixing but are from higher up in the framework or site layout for example.
I want to switch back to the main develop branch and commit changes from select files there, then go back to a feature branch, and rebase so that I can continue there with the unrelated tweaks/bugfixes out of the way.
I'm using git stash
and git stash pop
to do this, but I'm getting a lot of conflicts from a bunch of files I modifed but don't need to be committed to the parent branch anyway.
Is there another way to avoid conflicts or somehow save the current state, and only pull select working tree changes to another branch for commiting ? (sort of like git-stash-cherry-pick ;-))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
master
中提交您想要的更改。master
分支并使用gitcherry-pick
将更改移动到master
master
on your current branch.master
branch and usegit cherry-pick
to move the changes ontomaster
rebase
(optional)我通常会反过来做。我继续在我的功能分支中工作,直到我准备好在那里提交。一旦我这样做,我就会将属于该分支的新提交的所有更改添加到索引,但不添加属于 master 的更改。 git add -p 等让这变得非常简单。一旦索引中的所有相关更改,我都会提交到分支。所有剩余的脏更改都属于 master,一旦我切换到 master,就会很好地进行,这样我就可以在那里提交它。
I usually do it the other way around. I continue working in my feature branch until I'm ready to make a commit there. As soon as I am, I add all changes that belong into the new commit for the branch to the index, but not those that belong into master.
git add -p
et.al make that really easy. Once all relevant changes in the index, I commit to the branch. All remaining remaining dirty changes belong to master and will be carried along just fine once I switch to that so I can commit it there.尝试使用
--merge
选项切换回master
分支。它将尝试在两个分支之间进行三向合并。 git 文档有一个很好的例子:Try switching back to
master
branch using--merge
option. It will try to do a three way merge between the two branches. The git documentation has a good example:在 MacOS 上, GitX 使得执行 rafl 描述的那种选择性提交变得非常容易,所以这是一个好方法如果这就是您所处的环境,请接近它。
也可以/实用地在单独的提交中提交分支 y 更改和主 y 更改,然后使用
git format-patch
将分支中的提交导出为文件和git am
来拉取它们到主人那里。这里的危险是,如果更改周围的文件差异太大,在这种情况下,将提交拉入主服务器时可能会发生冲突。
On MacOS, GitX makes it very easy to do the kind of selective committing rafl describes, so that's a good way to approach it if that's the environment you're in.
It's also possible/practical to commit branch-y changes and master-y changes in separate commits, then use
git format-patch
to export the commits from the branch as files andgit am
to pull them in to the master.The danger here is if the files around the changes are too different, in which case there may be conflicts when pulling the commits in to the master.
创建一个临时分支怎么样?
像这样的东西:
What about creating a temporary branch?
Something like: