! [rejected]master -> master error: failed to push some refs to’https://github.com/ The ultimate solution

Git error:

$ git push -u origin master

To [email protected]:***.git

 ! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to '[email protected]:***.git

hint: Updates were rejected because the tip of your current branch is behin

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Git already has some code in the repository, so it doesn’t allow you to overwrite your code directly. There are two ways to solve this problem:
method 1: force the code to github warehouse (recommended, save time and effort)

git push -f origin master		//直接强推,即利用强覆盖方式用你本地的代码替代github仓库内的内容

method 2: fetch git’s stuff to your local, merge and push

git fetch
git merge			//这两条命令等价于git pull  
git config branch.master.remote origin  
git config branch.master.merge refs/heads/master  

Then pull git back and git pushes your code. Reference: Here.

Read More: