I'm new to GitHub. Today I met some issue when I was trying to push my code to GitHub.

Pushing to [email protected]:519ebayproject/519ebayproject.git
To [email protected]:519ebayproject/519ebayproject.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:519ebayproject/519ebayproject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I have not pushed anything in the repository yet, so why do I need to pull something?

Solution 1

This can cause the remote repository to lose commits; use it with care.

If you do not wish to merge the remote branch into your local branch (see differences with git diff), and want to do a force push, use the push command with -f

git push -f origin <branch>

where origin is the name of your remote repo.

Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check. This can cause the remote repository to lose commits; use it with care.

Solution 2

As the message tells you,

Merge the remote changes (e.g. 'git pull')

Use git pull to pull the latest changes from the remote repository to your local repository. In this case, pulling changes will require a merge because you have made changes to your local repository.

I'll provide an example and a picture to explain. Let's assume your last pull from origin/branch was at Commit B. You have completed and committed some work (Commit C). At the same time, someone else has completed their work and pushed it to origin/branch (Commit D). There will need to be a merge between these two branches.

local branch:                         --- Commit C 
                                    /
                                   /
                                  /
origin/branch: Commit A ------ Commit B ---- Commit D

Because you are the one that wants to push, Git forces you to perform the merge. To do so, you must first pull the changes from origin/branch.

local branch:                         --- Commit C -- Commit E
                                    /               /           
                                   /               /             
                                  /               /               
origin/branch: Commit A ------ Commit B ---- Commit D 

After completing the merge, you will now be allowed to fast-forward origin/branch to Commit E by pushing your changes.

Git requires that you handle merges yourself, because a merge may lead to conflicts.

Solution 3

Have you updated your code before pushing?

Use git pull origin master before you push anything.

I assume that you are using origin as a name for your remote.

You need to pull before push, to make your local repository up-to-date before you push something (just in case someone else has already updated code on github.com). This helps in resolving conflicts locally.

Solution 4

This normally happens when you git commit and try to git push changes before git pulling on that branch x where someone else has already made changes.

The normal flow would be as below,

STEP 1: git stash your local uncommitted changes on that branch.

STEP 2: git pull origin branch_name -v to pull and merge to locally committed changes on that branch (give this merge some message, and fix conflicts if any.)

STEP 3: git stash pop the stashed changes (Then you can make commits on popped files if you want or push already committed changes (STEP4) first and make new commit to files later.)

STEP 4: git push origin branch_name -v the merged changes.

Replace branch_name with master (for master branch).

Solution 5

First and simple solution:

  • Try this command git push -f origin master.
  • This command will forcefully overwrite remote repository (GitHub)

Recommended Solution 1 :

  • Run these commands:
git pull --allow-unrelated-histories  //this might give you error but nothing to worry, next cmd will fix it
git add *
git commit -m "commit message"
git push

If this doesn't work then follow along

Solution 2 (Not Recommend) :

Will Delete all your & your team-mate's commit history. So please dont do this on professional project

  • Delete .git directory from the folder.

  • Then execute these commands:

      git init
      git add .
      git commit -m "First Commit"
      git remote add origin [url]
      git push -u origin master
    

OR

git push -f origin master 

Only use git push -f origin master if -u dont work for you.

This will solve almost any kind of errors occurring while pushing your files.

Solution 6

Sometimes we forgot the pulling and did lots of works in the local environment.

If someone want to push without pull,

git push --force

is working. This is not recommended when working with other people, but when your work is a simple thing or a personal toy project, it will be a quick solution.

Solution 7

Some of you may be getting this error because Git doesn't know which branch you're trying to push.

If your error message also includes

error: failed to push some refs to '[email protected]:jkubicek/my_proj.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you did not intend to push that branch, you may want to
hint: specify branches to push or set the 'push.default' configuration
hint: variable to 'current' or 'upstream' to push only the current branch.

then you may want to follow the handy tips from Jim Kubicek, Configure Git to Only Push Current Branch, to set the default branch to current.

git config --global push.default current

Solution 8

git pull origin branch_name --rebase

This worked for me -- the command git pull origin branch_name --rebase will pull changes from remote branch_name at first, then rebase current branch on the top of it.

Solution 9

In addition to the answers above, the following worked for me : -

Scenario -

  1. I pushed my_branch to origin successfully.
  2. I made few more changes.
  3. When I tried to push again, (after doing add, commit of course), I got the above mentioned error.

Solution -

 1. git checkout **my_branch**
 2. git add, commit your changes.
 3. git pull origin **my_branch** (not origin, master, or develop)
 4. git push origin **my_branch**

Solution 10

I had the same problem , what I did was I first pushed it by force by using this

git push --force

I did this after I commited the files and was getting an error as you got.It did commit all the files and it pushed them. Then the next time I was pushing to the github .I did what it asked me to and it was alright then. Hope this works for you too :)

Solution 11

I mentioned this in my tutorial, How To Use GitHub: A tutorial for beginners.

When you create a new repository on GitHub, GitHub may ask you to create a readme file. If you create a readme file directly on GitHub, then you will need to first make a pull request before the push request will be successful. These commands will pull the remote repository, merge it with your current files, and then push all the files back to GitHub:

git pull https://github.com/thomas07vt/MyFirstRepo.git master

git push https://github.com/thomas07vt/MyFirstRepo.git master

Solution 12

git push -f origin branchname

Use the above command only if you are sure that you don't need remote branch code otherwise do merge first and then push the code

Solution 13

I was getting the above mentioned error message when I tried to push my current branch foobar:

git checkout foobar
git push origin foo

It turns out I had two local branches tracking the same remote branch:

foo -> origin/foo (some old branch)
foobar -> origin/foo (my current working branch)

It worked for me to push my current branch by using:

git push origin foobar:foo

... and to cleanup with git branch -d

Solution 14

Your branch should include the latest merged changes as soon as you notice them, but you haven't pulled the latest changes.

git fetch 

might be all that is needed. If that doesn't work, then you might need to:

git pull <sharedRepo> <branch> --rebase

If you don't have any merge conflicts, you should be able to push your changes successfully.

git push <forkedRepo> <branch>

If you encounter merge conflicts, you cannot solve them remotely in GitHub. You have to solve them locally and then push the resolutions with a force label because a merge conflict resolution changes history.

git push <forkedRepo> <branch> -f

Solution 15

Just had the same issue but in my case I had typed the wrong branch on the remote. So, it seems that is another source of this issue... double check you're pushing to the correct branch.

Solution 16

I experienced the very same problem and it turned out I was on a different (local) branch than I thought I was AND the correct local branch was behind in commits from remote.

My solution: checkout the correct branch, cherry-pick the commit from the other local branch, git pull and git push

Solution 17

I had a similar issue and it turned out that my workflow for keeping my branch up to date was at fault. I was doing the following:

In my local 'master'

git fetch upstream
git merge upstream/master --ff-only

then back in my local branch

git rebase master

This worked well for a previous git flow but not with github. The git rebase was the problem here causing issues with syncing (and I'll admit that's something I've had to accept without fully understanding) and unfortunately put me in a position where git push -f became probably the easiest option. Not good.

My new flow is to update the branch directly using git merge as follows:

In my local branch

git fetch upstream
git merge upstream/master

No fast forward, as I will have made changes of course in the local branch.

As you can probably tell, I'm no git expert but I'm reliably informed that this workflow will probably avoid the specific problems that I had.

Solution 18

In my case, I had "mybranch" checked out, and had done git pull, so I couldn't figure out why the push wasn't working. Eventually, I realized that I was pushing the wrong branch. I was typing git push origin master instead of git push origin mybranch.

So if you've already done git pull and still getting this message, make sure you're pushing the correct branch.

Solution 19

Is your branch name the same as the remote branch name?

If no, you should checkout a new branch with the same name as the remote branch and try push it again.

Assume the remote branch you want to push is [testing] and your local branch is named as [test].

If you`re not in test branch, first switch to it.

git checkout test

Then open a new branch and name it testing.

git checkout -b testing

Now, it`s time to push it:

git push [remote repo] testing

Solution 20

If you are certain that no one made changes to your git repository and that you are working on the latest version, git pull doesn't make sense as a solution in your heart...

Then this is probably what happened, you used git commit --amend

It lets you combine staged changes with the previous commit instead of committing it as an entirely new snapshot. It can also be used to simply edit the previous commit message without changing its snapshot.

ATLASSIAN tutorial: rewriting history

However, it is not recommended to perform git commit --amend if you have already pushed the commit to GitHub, this is because "amending doesnt just alter the most recent commitit replaces it entirely. To Git, it will look like a brand new commit" which means to other developer on your GitHub, history looks like A->B->C but to you it looks like A->B->D, if GitHub let you push, everyone else will have to manually fix their history

This is the reason why you get the error message ! [rejected] master -> master (non-fast-forward), if you know that no one has pulled your latest change, you can do git push --force, this will alter the git history in your public repo. Otherwise...you can perform git pull, but I believe this will have the same result as you didn't go through git commit --amend,it will create a new commit (ie: git history after git pull: A->B->C->D)

for more detail: How to change your latest commit

Solution 21

I have resolve this issue at my GIT repository. No need to rebase or force commit in this case. Use below steps to resolve this -

local_barnch> git branch --set-upstream to=origin/<local_branch_name> 

local_barnch>git pull origin <local_branch_name>

local_barnch> git branch --set-upstream to=origin/master

local_barnch>git push origin <local_branch_name>

hope it will help.

Solution 22

Another solution is to advance the head of the remote by making another commit if you can. After you pull this advanced head into the local subtree then you will be able to push from it again.

Solution 23

I was getting a similar error while pushing the latest changes to a bare Git repository which I use for gitweb. In my case I didn't make any changes in the bare repository, so I simply deleted my bare repository and cloned again:

git clone --bare <source repo path> <target bare repo path>

Solution 24

Another option: locally rename your branch to something new.

You will then be able to push it to the remote repository, for example if that is your way of keeping a copy (backup) and making sure nothing gets lost.

You can fetch the remote branch to have a local copy and examine the differences between (i) what the remote had (with the old branch name) and (ii) what you have (with the new branch name), and decide what to do. Since you weren't aware of the remote's differences in the first place (hence the problem), simply merging or forcing changes somewhere is far too brutal.

Look at the differences, pick which branch you want to work on, cherry pick changes you want from the other branch, or revert changes you don't want on the branch you've got etc.

Then you should be in a position to decide whether you want to force your clean version onto the remote, or add new changes, or whatever.

Solution 25

The problem with push command is that you your local and remote repository doesn't match. IF you initialize readme by default when creating new repository from git hub, then, master branch is automatically created. However, when you try to push that has no any branch. you cannot push... So, the best practice is to create repo without default readme initialization.

Solution 26

This problem is usually caused by creating a readme.md file, which is counted as a commit, is not synchronized locally on the system, and is lacking behind the head, hence, it shows a git pull request. You can try avoiding the readme file and then try to commit. It worked in my case.

Solution 27

Another cause of this problem (apparently not so common)...

My server was behind ~12 hours when I did a push

I configured NTP on the server SYNC my clock.

I executed a new git push which led the error discussed in this post.

Solution 28

If by any chance git pull prints Already up-to-date then you might want to check the global git push.default param (In ~/.gitconfig). Set it to simple if it was in matching. The below answer explains why:

Git - What is the difference between push.default "matching" and "simple"

Also, it is worth checking if your local branch is out of date using git remote show origin and do a pull if needed

Solution 29

use git pull https://github.com/username/repository It's because the Github and remote repositories aren't in sync. If you pull the repo and then Push everything will be in sync and error will go away.

`

Solution 30

You won't be able to push changes to remote branch unless you unstage your staged files and then save local changes and apply the pull from remote and then you can push your changes to remote.

The steps are as follows-->

git reset --soft HEAD~1 ( to get the staged files)

git status (check the files which are staged)

git restore --staged <files..> (to restore the staged)

git stash (to save the current changes)

git pull (get changes from remote)

git stash apply (to apply the local changes in order to add and commit)

git add <files> (add the local files for commit)

git commit -m commit msg

git push