1

I am getting the following error on pushing the local git repo to empty remote repo. It's a private repo.

$ git push origin master To https://bitbucket.org/lord_of_codes/uat_api.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://lord_of_codes@bitbucket.org/lord_of_codes/uat_api.git' hint: Updates were rejected because the tip of your current branch is behind 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.

I just created a empty git repo on bit bucket.
I did git init in my local project
I did git add .
I commit for first git commit -m "Initial Commit"
I did git config --global user.name git config --global user.email
I did git remote add origin url I did git push origin master
Then I got that error.
I also tried pushing codes before commit, and also tried git pull. But It's not working

deepdark
  • 103
  • 1
  • 8

3 Answers3

2

This is a non-fast-forward error. Github has a small section explaining this, check it out here.

Basically what git's doing is preventing you from writing into a repo whose head it different from yours. It means your repo (https://bitbucket.org/lord_of_codes/uat_api.git) has commits that are different from the one in your PC/local code and you're trying to add more commits/changes to a codebase which is different from yours.

According to GitHub you can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:

    $ git fetch origin
    # Fetches updates made to an online repository
    $ git merge origin YOUR_BRANCH_NAME
    # Merges updates made online with your local work

Or, you can simply use git pull to perform both commands at once:

    $ git pull origin YOUR_BRANCH_NAME
    # Grabs online updates and merges them with your local work

In other 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.
Source

For more understanding on git refer to this amazing "A Visual Git Guide"

clamentjohn
  • 3,417
  • 2
  • 18
  • 42
1

You can push forcefully to the remote branch which will solve the issue.

git push -u origin master # without --force

if that does not work, since it's an empty repository created by you, you can do

git push -u -f origin master # with force
Knight Rider
  • 972
  • 2
  • 10
  • 22
  • What if the data in `remote` is important? You should mention the consequences of a `--force`. – clamentjohn Aug 03 '19 at 08:51
  • If the remote is important, there is always a way to resolve such issues. Either by pulls with conflict resolutions or allowing unrelated histories along the way depending on the situation but in this case, the remote is empty. which is fine but the --force will erase histories. – Knight Rider Aug 03 '19 at 09:00
  • Remote is not empty! I think you already knew that. This might be a new git user and he/she might not know how git works. Consider a repo used by people other than OP, if OP just copy-pasted your answer the team's previous commit history would be lost. – clamentjohn Aug 03 '19 at 09:19
  • I knew remote is empty from the question. The user created the repo and that is a fresh one. – Knight Rider Aug 03 '19 at 09:29
0

I have a similar problem:

(! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@it-bucket:some-project/some-repo.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.)

when i am pushing my local repository to the remote repository.

Problem: I was creating the new repository on git-bucket and I initialized with README file which actually doesn't match with my local repository.

Solution: I delete the existing repository OR create a new repository and doesn't initialize with README file then add the following commands

git remote add origin "repository URL"

and then

git push -u origin master
muescha
  • 1,544
  • 2
  • 12
  • 22
Usama Zafar
  • 77
  • 1
  • 3