1
$ git push -u origin master
To https://github.com/XXXXXXXXX.com/FirstRepo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/XXXXXXXXX.com/FirstRepo.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 am using github for the first time today, I had already checked other answers out of which only one seems to work and that is using 'git push origin master --force', but by doing this it is deleting all other commits. So can anyone please tell me the solution for this.

P Yoga Prudhvi
  • 107
  • 2
  • 3
  • 11

3 Answers3

5

If your local branch is behind your remote branch and have some commits on top of it, then you can solve your problem by running the following commands.

$ git pull origin master --rebase
$ git push origin master

NB: If you only run git pull without --rebase, then it will add a merge commit which you probably don't want.

Masudur Rahman
  • 1,585
  • 8
  • 16
1

Just run this code in terminal .

git pull origin master --allow-unrelated-histories

or if you use main branch run this code

git pull origin main --allow-unrelated-histories
Abdullah alkış
  • 325
  • 4
  • 13
0

Although it sounds like it didn't work for the OP, as Deepak Patankar said in his comment, you may want to try a

git fetch

first. It seems like an easier place to start than special parameters on a pull command.

Programmer Paul
  • 488
  • 1
  • 3
  • 13