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:
Written by Dave Griffiths
Related protips
12 Responses
Doesn't this give you a lot of "merge branch" in your log?
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.
Why don't use rebase
instead of merge
?
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)
Also, / is totally valid in a refname, so if you've named your branch bugs/661-descriptionofbug it'll explode.
@richoh True, and not uncommon. For instance git-flow will always name branches: feature/this
, feature/that
, hotfix/foo
Why not just git pull --rebase origin master
from your feature branch.
@clowder and even git config alias.update 'pull --rebase origin master '
and then running just git update
.
This script doesn't jump back to original branch when run in GitBash for Windows.
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>
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.