clueless coding // TODO: be smarter


Updating a Feature Branch with Master in Git

Keeping Your Feature Branch Up-To-Date

A common source control development practice is to develop on feature branches, and then make pull requests to master. Unfortunately, while any given feature is being developed, master could be constantly updated with work implementing other features.

This can cause problems, especially if you’re running a suite of tests that touches your feature as well as other parts of the code. Databases might change, API functionality on other services might change, tests might change. This could all be accounted for on master - where features and bug fixes were merged in together - but because your feature branch is out of date, you might run into some issues.

Thankfully, git offers features that solves these exact problems.

To consistently keep our feature branch up to date with master, we can run these commands:

git checkout -b feature-branch
git stash
git checkout -b master
git pull
git checkout -b feature-branch
git rebase origin/master
git stash pop



Updating Master, and then Updating Another Branch

Our issue arises when we have a branch that isn’t up to date with master.

feature branch with uncommited changes

In the picture above, we’ll treat our green bubbles as commits on our feature branch. The green bubble with a red outline signifies uncommitted changes on our branch.

Clearly, we’re missing a couple of blue bubbles - commits on master - that we might need. In general, it’s good practice to update our feature branch to contain those commits. Ideally, after updating, our branches would look like so:

feature branch with uncommited changes

Our feature is completely up to date with master, and it looks like we just branched off the head of master.

To do that, we first will stash our changes. That simply keeps our uncommitted changes in storage for later use. In our diagram, it holds our red outline green bubble in storage.

git checkout -b feature-branch
git stash

feature branch with uncommited changes

Our next step is to update our local master to match the remote. We’ll swap over to our local master and simply run a git pull to receive updates.

git checkout -b master
git pull

local branch fully updated to match master branch

We then need to swap back to our feature branch, and rebase it on top of master. Rebasing a branch does exactly what its name implies - it replays the changes on your branch on top of a new ‘base’ - in this case, we’re going to set our new base as our (freshly updated) master, and then put our changes on top of it.

NOTE: If your branch has merge conflicts, you’re going to need to resolve them here. You might as well do it now, because you would have had to while merging your code in regardless.

git checkout -b feature-branch
git rebase origin/master

rebased feature branch on top of updated master

Our last step is to replay those uncommitted changes we stashed earlier.

git stash pop

pop stash on top of rebased feature branch

That’s it! We’ve successfully updated not only our local master, but our feature branch as well. This technique allows us to keep our feature branch completely clean - we can always develop on a ‘clean’ branch, and not have to worry about the code that we’re not touching becoming stale.