9idt5g
Last Updated: February 25, 2016
·
48.05K
· davegriffiths
18570259820f61a19e72c619f16e482f

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.

Say Thanks
Respond

12 Responses
Add your response

735
C61af3cc30d517df9e00835697829bc0

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

over 1 year ago ·
737
3210e1be3e4059b93c4a88309e2183a2

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 ·
738
7ab54d8e4963df0342cd5e8522335b82

Why don't use rebase instead of merge?

over 1 year ago ·
740
18570259820f61a19e72c619f16e482f

@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 ·
741
Richhealeyavatar

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 ·
742
Richhealeyavatar

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

over 1 year ago ·
743
3210e1be3e4059b93c4a88309e2183a2

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

over 1 year ago ·
744
A9f136bb1099c410158a6119c0633a40

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

over 1 year ago ·
821
Me

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

over 1 year ago ·
13415
780bd621fc8dd496f89ffd2b5f141296

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

over 1 year ago ·
14248
Avatar normal

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 ·
16879
B16df3763f705c77f674a72fecfa32ef normal

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 ·
Filed Under