'error: failed to push some refs to 'https://github.com/XXXXXXXXX.com

$ 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.



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Masudur Rahman
Solution 2 Abdullah alk??
Solution 3 Programmer Paul