Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it neccessary to update git branch before merging it to develop / master?

Tags:

git

git-merge

Consider a scenario in which I have created a branch from master called "Feature 1". My friend also created a branch from master called "Feature 2". He finished his code changes and merged "Feature 2" branch back to master.

Now I have completed my changes in "Feature 1" branch and want to merge back to master ( which has been updated since I branched of it ). Do I need to pull / rebase before I can merge "Feature 1" to master or is that something git will handle?

like image 279
Shaggy Avatar asked May 19 '14 23:05

Shaggy


1 Answers

It's not necessary to update your master branch before you merge your feature branch into it. However, that is not the best practice. You are better off doing the following:

  1. Pull master branch and sure it is up to date with your remote.
  2. Merge/rebase your master branch into your feature branch
  3. Fix any merge conflicts
  4. Merge your feature branch into master

Doing it this way will ensure that your commits are the most recent in the history and that any merge conflicts are handled on the feature branch, not the master branch. This keeps your master branch clean and your history cleaner. The other people using your repo will be happy.

The commands will look like this:

  1. git checkout master
  2. git pull --rebase origin master
  3. git checkout feature1
  4. git rebase master
  5. fix any conflicts
  6. git checkout master
  7. git rebase feature1 // will rebase with no conflicts since you fixed them previously.

To elaborate on what each command will do:

  1. jump to your version of master, which is outdated
  2. pull changes from repo and update master, applying any local changes master has on top (none, if you used branches for edits)
  3. jump back to your feature1 branch that's still based off the outdated "master"
  4. apply feature1 changes on top of master, which was updated in #2
  5. fix conflicts
  6. jump back to master, which is updated but still without your feature1 changes
  7. apply master changes on top of feature1, but since feature1 is a direct child of master, this just updates master again with your feature1 changes
  8. here you should consider git push to "publish" your changes to master to the repo
like image 199
Andrew Eisenberg Avatar answered Nov 11 '22 22:11

Andrew Eisenberg