How to update a Git branch

This post goes over how to update a Git branch:


Given you’re on branch feature:

git branch
* feature

And there are new commits on origin/master:

git fetch
   abc12345..def67890  master -> origin/master

How would you go about merging the commits to your feature branch?


To merge the commits:

git merge origin/master

If there are differences, the merge will apply the commits to the top of feature and create a new merge commit. Otherwise, the merge will be resolved by a fast-forward.


To rebase the commits:

git rebase origin/master

Rebase moves all diverging commits of feature to the top.

This means that the diverging commits will have new commit hashes because history will be rewritten.

Also, if you’ve previously pushed your feature branch to remote, then you need to force push to update it:

git push origin feature --force

However, if developers have checked out your feature branch, then I don’t recommend this method. Stick with merge.

Please support this site and join our Discord!