Last Updated: July 12, 2021
·
113.7K
· davegriffiths

Keep your feature branch up to date.

Beavering away on a new feature in a separate branch? That's good, but you'd better make sure you keep pulling in the changes from the master branch incrementally so you don't end up with a gargantuan merge full of conflicts later.

Put this function in your .bashrc or .zshrc. It automates it for you.

function update(){
  git checkout master && git pull && git checkout - && git rebase master
}

Type update in the terminal whilst in your feature branch. This is what it does:

1) Checks out the master branch.
2) Pulls any remote changes.
3) Swaps back to your original branch.
4) Rebases with master.

Hopefully it will save you some time.

Related protips:

Remove all your local git branches but keep master

12 Responses
Add your response

Doesn't this give you a lot of "merge branch" in your log?

over 1 year ago ·

You can just use git checkout - to jump back to the $branch, so that you don't have to figure it out by parsing.
Also, consider using rebase so that your work is replayed onto master. This way should a merge conflict arise, it's easier to determine where your patches go.

over 1 year ago ·

Why don't use rebase instead of merge?

over 1 year ago ·

@treggats Yes it does, but not if I you use rebase...
@kvz Cheers, works a treat, I will update the tip.

over 1 year ago ·

Even better, just git fetch && git merge origin/master (subbing origin for whatever you named the remote you're interested in).. or for master git-fu master@{u} will use master's upstream tracking branch on versions of git later than somewhere around 1.7 (I forget where this syntax was introduced)

over 1 year ago ·

Also, / is totally valid in a refname, so if you've named your branch bugs/661-descriptionofbug it'll explode.

over 1 year ago ·

@richoh True, and not uncommon. For instance git-flow will always name branches: feature/this, feature/that, hotfix/foo

over 1 year ago ·

Why not just git pull --rebase origin master from your feature branch.

over 1 year ago ·

@clowder and even git config alias.update 'pull --rebase origin master ' and then running just git update.

over 1 year ago ·

This script doesn't jump back to original branch when run in GitBash for Windows.

over 1 year ago ·

here is how i do it. Then when I am ready to push to origin you can squash commits or whatever.

Switch to your clean branch that you keep synced to origin. Rebase it from orgin. Merge that into your feature branch.
<cope>
git checkout mycleanclone
git fetch
git pull --rebase
git checkout myfeature
git merge mycleanclone</code>

over 1 year ago ·

Doesn't this rebase your feature branch changes onto master? Isn't that exactly the opposite of what you wanted, which was to rebase the changes made on master onto your feature branch?

I'm trying to keep my feature branch up to date with changes on master, but whenever I try to rebase I get changes on master instead of on the feature branch. It's weird because I can successfully rebase stuff onto my master.

I'm obviously very confused about something. Hope you can help.

over 1 year ago ·