81

I have a project which uses git and I'd like to start a new branch to add a major new feature.

Under the main branch, I'll continue to add bug fixes and minor features. At regular intervals I'd like to pull the changes from the main branch into my "major new feature" branch. What's the best way to do this?

Eventually, I'll merge the "major new feature" branch into the main branch.

Scott Anderson
  • 631
  • 5
  • 26
FunLovinCoder
  • 7,597
  • 11
  • 46
  • 57

2 Answers2

114
git checkout featurebranch && git rebase master

As long as you haven't pushed yet, it is better to replay your changes on top of master.

See:

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Can you get more recent changes with git rebase, or only with git merge? – keflavich Jun 28 '12 at 21:56
  • @keflavich since you are rebasing `featurebranch` on top of master, you are getting all the latest commits from `master`. A merge would be similar but will make for a slightly more complex integration with `master` later on. – VonC Jun 28 '12 at 22:02
  • 2
    hey! I'm a git newb, so can you go into more detail on `as long as you haven't pushed yet?` I've got a feature branch that I've pushed to a remote repo for backup. Can I keep doing `git rebase master`, then commit and push some work to my feature branch, then `git rebase master` ? I have to merge other feature branches into my branch as well too! – Brad Parks Jul 24 '14 at 10:23
  • @GringoSuave Yes, if you are not the only one working on that feature branch. Generally, rebase only what you have not pushed yet (unless, again, you are the only one working on it) – VonC Aug 28 '18 at 06:06
55

git checkout featurebranch && git merge master

You can do this as many times as you like; it won't affect master and you will be able to easily do it the other way around whenever you find out you are done with the feature branch.

David Winslow
  • 8,472
  • 1
  • 31
  • 27
  • 29
    `git rebase master` may be more desirable than `git merge master` for simplifying later integration back into the master branch. – Amber Jun 26 '10 at 16:55