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/
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
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.
git config branch.{{ branch name }}.rebase true
git pull --no-rebase
Recent Comments