From time to time, you may encounter this error while pushing commits to GitHub:
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.
This error can be a bit overwhelming at first; do not fear!
Simply put, Git can't make the change on the remote without losing commits, so it refuses the push. Usually, this is caused by another user pushing to the same branch.
You can remedy this by fetching and merging the remote branch:
git fetch origin git merge origin master
Or, you can simply use
git pull to perform both commands at once:
git pull origin master
In some cases, this error is a result of destructive changes made locally by
using commands like
git commit --amend or
git rebase. While you can override
the remote by adding
--force to the push command, you should only do so if you
are absolutely certain this is what you want to do. Force-pushes can cause issues
for other users that have fetched the remote branch, and is considered bad practice.
When in doubt, don't force-push.
Since GitHub also supports access via Subversion clients, the behavior of accessing a Git repository as an SVN repository is a bit different.
Subversion is configured to assume that history never changes, which is not the
case with Git (especially when using
--force). Thus, if you've
wiped away some commit history with Git, those commits will remain in the SVN