3

I work at a Git project.

My further work plan is to switch from main branch to to "devel" branch where I develop new features.

But it is possible that sometimes while working in "devel" branch I may spot an error. If I spot an error, I should correct it not only in "devel" but in "main" also.

It is not convenient to switch from "devel" to "main" (and back) every time I find an error.

How to do it with ease?

porton
  • 5,214
  • 11
  • 47
  • 95

1 Answers1

2

If you find a bug that should be fixed in both master and develop I would recommend making the fix in master as a single commit and then rebasing your develop branch on top of master.

So, a rebase-workflow would look like this:

git checkout master
# fix bug
git add fileYouAltered
git commit -m "fixed bug"
git push origin master

git checkout develop
git rebase master
git push -f origin develop

A merge-workflow would look like this:

git checkout master
# fix bug
git add fileYouAltered
git commit -m "fixed bug"
git push origin master

git checkout develop
git merge master
git push origin develop

Now the fix will be in both branches but with only a single commit.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • Thanks. But I also need `git push` to update my repo at BitBucket. Can this be done with one `git push` or do I need to run `git push` twice (for each of the two branches)? Please update your answer with adding `git push` – porton Feb 09 '16 at 15:54
  • @porton I updated my answer to show the push to master, but the push to develop is a little more tricky because the rebase may have re-written the history of the branch. For that you may need to force-push. This blog entry may be a good starting point to understanding the ramifications of that: http://willi.am/blog/2014/08/12/the-dark-side-of-the-force-push/ – Jonathan.Brink Feb 09 '16 at 15:57
  • What's about using `merge` instead of `rebase` in order to simplify pushing? Is it a good idea? I am not an expert on differences of `merge` and `rebase` and need an advice – porton Feb 09 '16 at 16:03
  • @porton, yes merge could definitly be an option. I personally reach for rebase more often to maintain a more linear history, but other people may prefer merging. Just depends on your situation. If you are the only developer on the `develop` branch then force pushing should be harmless – Jonathan.Brink Feb 09 '16 at 16:06
  • Could you write also "merge" workflow, to show both possible variants? I'd like also to see pros and cons of both variants – porton Feb 09 '16 at 16:40
  • "Now the fix will be in both branches but with only a single commit." Does this apply to the `merge` workflow? (just to be sure that you haven't introduced a logical error while editing the answer) – porton Feb 09 '16 at 16:50
  • @porton Yes, that comment applies to both approaches – Jonathan.Brink Feb 09 '16 at 16:51
  • Please add all necessary `git push` to the workflows for the remote repository `origin` – porton Feb 09 '16 at 17:23