1

Previously, using SVN, I would have a branch that was my development branch, which matched the code on the development site. Once in a while, I would make a change in the development branch and then merge just the change into the trunk so I could put it on production. I'm wondering how I might accomplish something similar with git.

Basically, I want to be able merge 1 or a few commits from the branch into the master without merging the entire branch.

Or should I be working with git differently? (I need to release the changes right away so I can't way till all the changes are done.) Should I be working with multiple branches? Can I merge 1 branch into multiple other branches?

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261

2 Answers2

2

You've almost answered your question: yes, you can merge one branch into multiple other branches.

So, what you want to do is create a branch just for this feature/bugfix (the common name is "topic"), starting from a common ancestor of all branches you'll want to merge it into. Do your work, commit it, then merge it into all of those.

# say version 1.2.3 is a common ancestor, and it's tagged
git checkout -b bugfix v1.2.3

# do some stuff
git add ...
git commit

git checkout master
git merge bugfix

git checkout dev
git merge bugfix
...

The key part here is to make sure to start your branch at a common ancestor. If you don't, you'll end up merging pieces of other things in as well.

If for some reason it's difficult to find a good common ancestor, you can fall back to cherry-picking. This essentially copies a commit from one place to another. It's best to avoid it when you can, though, since it does mean you end up with two copies of a commit in the history.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
1

I prefer to make "topic branches" in git, where I implement a feature in its own branch. Then I can choose if I merge it into development or production or both. This allows feature development to continue, while bug fixes can still be applied to production in a reasonable amount of time.

Its similar in concept to the model explained here: http://nvie.com/posts/a-successful-git-branching-model/

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71