Simple rebase/merge workflow and how to deal with possible merge conflicts.
So you're building a feature on a branch called some-feature-branch, and it
takes you, say, three days to finish. While you were busy, your co-worker merged
their feature into master, so there is new code in master that you need to
bring into your branch. A handy way to do that is to rebase the changes to
some-feature-branch before merging some-feature-branch to master.
Here's how that workflow goes.
Rebase master to feature-branch
$ git checkout master
# Pull in the new stuff
$ git pull origin master
# Checkout your feature and rebase master to it
$ git checkout some-feature-branch
$ git rebase masterNext, Git will try to move the new commits in master to the tip of your
some-feature-branch. Here's what might happen:
- Hopefully not, but most likely yes, Git will whine about merge conflicts.
- Solve the conflicts by opening the files that have conflict, remove the inserted markers, and make it look like you want it to be.
$ git add <file-name>or in one clump$ git add -A.$ git rebase --continue.- Rinse and repeat until Git stops whining.
Dealing with merge conflicts in dist files
You might have CSS or JavaScript files in the dist/ directory (or build, or
whatever you named it) that are generated automatically by a build task, like
Sass or r.js. You can exclude the whole dist dir from the repo, but if you're
like me, and use Git to deploy the project also, that's not doable.
I haven't found a good solution other than blindly adding all files in dist
and continuing with the rebase. Then, of course, run the build script after the
rebase is done.
$ git add dist/$ git rebase --continue
Now there's tons of merge conflicts in your dist/*.css and dist/*.js files
and that's why you need to run your build task again.
We're done rebasing. Great! Let's look at merging the feature branch into master.
Merge feature branch to master
This should be a cakewalk now, since we just resolved all the merge conflicts, all you need to do is:
$ git checkout master
$ git rebase some-feature-branch
## Conclusions
If you're interested I have a more [detailed article about rebasing and merging](/git-dealing-with-branches-merging-and-rebasing).