Friends link: 200-120 100-101 352-001 70-410 200-125 070-411 200-101 400-101 C2020-703 70-412 642-747 70-417 200-120 70-487 350-018 352-001 300-207 117-201 210-260 SY0-401 200-125 300-320 200-310 300-075 70-534

Avoiding Git Merge Commits with Rebasing

February 25th, 2015
Posted by
Categories: Technology

Let’s talk about Merge Commits. Inevitably, this error will happen to everyone (if it hasn’t many times in the past):

git push origin master
# To https://github.com/user/repo.git
#  ! [rejected]        master -> master (non-fast-forward)
# error: failed to push some refs to 'https://github.com/user/repo.git'
# To prevent you from losing history, non-fast-forward updates were rejected
# Merge the remote changes (e.g. 'git pull') before pushing again.  See the
# 'Note about fast-forwards' section of 'git push --help' for details.

Generally, the idea behind fixing that is to run git pull before git push. That’s not a bad solution, but it leaves useless “merge commits” in the commit log like this:

Merge branch 'master' of ssh://git.github.com/projectname/
Generally, I prefer to see merge commits only in the case of an actual merge, like a topic branch into a master branch. Commits like this duplicate all of the changes that you had to merge before you push your own commits.

Luckily, fixing that is easy! The solution is to put your commits on top of the updated master branch. Git has a built in solution: git pull –rebase. That command will pull the working branch, merge the changes before your changes, then apply your changes at the tip of master. That’s really convenient! It leaves the history much cleaner.

Another way of running the same commands is:

git pull
git rebase

You can configure git pull to always run as git pull –rebase using this command:

git config branch.autosetuprebase always
Looking at this page, there are a few options for autosetuprebase:

When a new branch is created with git-branch or git-checkout that tracks another branch, this variable tells git to set up pull to rebase instead of merge (see “branch..rebase”). When never, rebase is never automatically set to true. When local, rebase is set to true for tracked branches of other local branches. When remote, rebase is set to true for tracked branches of remote branches. When always, rebase will be set to true for all tracking branches. See “branch.autosetupmerge” for details on how to set up a branch to track another branch. This option defaults to never.

Note that the above command will only setup autorebase for NEW branches. To setup autorebase for existing branches, use:
git config branch.{{ branch name }}.rebase true
Where {{ branch name }} indicates where you type a branch name.
You can get into trouble using git pull –rebase in the same situations as git rebase, indicated on this page of the Git book. However, if a pull/rebase would cause a problem, you can always override the feature by running:
git pull --no-rebase

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Comments RSS