Two parallel branches in Git, how to work on “future” but commit basic fixes to “master”

我怕爱的太早我们不能终老 提交于 2020-12-30 02:35:06

问题


This is probably a common scenario but there are so many Git workflow questions here on SO that I couldn't find an answer for my exact question. Which is:

I have a master and future branches in Git that live in parallel. future contains some future features while master is the current version (approximately).

When developing a future feature, I often encounter a situation where I need to make a fix that needs to go to master. What I do now:

  1. Stash work in progress in future
  2. Switch to master
  3. Fix code in master
  4. Switch to future
  5. Merge from master
  6. Stash pop

In some cases, where the fix is better tested in the future branch, the process becomes even more cumbersome, consisting of making the change in future, committing it there, switching to master, cherry-picking from future, checking out future, reseting future and merging from master in the end.

There must be a better way but I can't figure it out. Approaches that wouldn't work (I believe; correct me if I'm wrong):

  • Merging from future to master. I don't want all commits from future in master.
  • Committing everything to future and then cherry-picking certain commits to master. This alone would not be enough because future merges from master to future would cause duplicate commits (or maybe even conflicts?). Maybe I could create opposite commits in future to those cherry-picked to master but that feels messy.

How do people deal with this workflow?


回答1:


If you are alone developing on the future branch, you could rebase it on top of master instead of merging master to it.

And since git 1.8.4 (July 2013), git rebase has learned to "autostash": see this answer.

git checkout future
git rebase --autostash master

For the opposite case, cherry-pick remains the main option.




回答2:


  1. Stash work in progress in future
  2. Switch to master
  3. Fix code in master
  4. Switch to future
  5. Merge from master
  6. Stash pop

There must be a better way but I can't figure it out.

I used to do that process, but after I found I needed to do switch back and forth often, I changed to simply having 2 working directories - one for working on "master" branch and one for "future" branch ("develop" in my case). Both dirs used the same remote repo/origin and the two working dirs had each other as remotes too. Two dirs eliminated the need to stash save/pop before/after switching branches.

I also didn't feel the need to pull the fix from master into future (step 5) right away unless there was a reason I had too. Normally, I would finish the current feature for future first and delay merging from master until convenient.




回答3:


You can just create a new branch for the fix.

  • Merge it in master
  • Merge it in your branch if it's not convenient to do it at that very moment directly to master.
  • Other developers could merge it in their own branches.


来源:https://stackoverflow.com/questions/27252967/two-parallel-branches-in-git-how-to-work-on-future-but-commit-basic-fixes-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!