268

I'm working on a team with a few developers using Git on Bitbucket. We are all working on a dev branch, not pushing to master until a release.

One of the developers committed incorrect code that overwrote my own by accident, and now I am trying to push the correct code back to the repository. I have been reading about this error for a few days now, and I can't push to the repository any more, because I am getting the following error:

 ! [rejected]        master -> dev (fetch first)
error: failed to push some refs to 'https://myusername@bitbucket.org/repo_user/repo_name.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I follow the instructions and pull, but then I receive a merge conflict. After entering a message for the merge conflict, my local code is now the incorrect code that the other developer uploaded by accident (as expected from the pull). So I replace the incorrect code with the backup I copied before committing, and when I try to push again, I get the same error.

How can I solve this issue?

These are the commands I run in order to commit:

git pull remotename master:dev
git add --all
git commit -m "some message"
git pull remotename master:dev
git push remotename master:dev

I would have thought that if I kept this order, I would not receive merge conflicts. I guess I was wrong.

I have looked for a few hours on Google and Stack Overflow, and followed different instructions, but I still can't do a Git push to the dev branch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thanos
  • 3,203
  • 4
  • 18
  • 27
  • this same error message is shown when you issue `git push` when currently in dir from another repo .... the git message should get updated to reflect this especially since its tone sounds so authoritative one might be convinced otherwise – Scott Stensland Mar 22 '21 at 15:53

21 Answers21

401

You can override any checks that Git does by using "force push". Use this command in the terminal:

git push -f origin master

However, you will potentially ignore the existing work that is in remote. You are effectively rewriting the remote's history to be exactly like your local copy.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Donal
  • 6,082
  • 2
  • 19
  • 30
  • 111
    Using force push (-f) flag is very dangerous and it should never be part of your regular work flow – Spaideri Apr 20 '17 at 07:13
  • 14
    Downvoted since I’m missing some warning in this answer. – Melebius Apr 19 '18 at 09:43
  • 5
    Ooh! This force the repository to rewrite itself. – Azarsa May 07 '18 at 11:35
  • 2
    Yeah this saved me since I didn't need _anything_ in the existing repo, but definitely a scary command – maxshuty Nov 07 '19 at 15:22
  • 3
    I had the same error with github and I fixed it with this command, @theeastcoastwest why are you saying this is dangerous? what is your reason" – simon Jan 07 '20 at 07:45
  • 11
    @simon this is dangerous because it ignores the work that is in remote and it forces your changes onto the repo. So if you don't want to mess up your team's work, DO NOT force push. – Gásten Feb 19 '20 at 13:30
  • 40
    Raise your hand if you did this and feel guilty but would do it again. – eric Jul 15 '20 at 17:40
  • 1
    This command helped me to escape the situation, but I will never use this command. ;) Thank you by the way. upvoted – VishalParkash Jul 22 '20 at 07:54
  • 2
    @Donal Please consider editing in a warning in this answer. [git push -force is evil](https://estl.tech/a-gentler-force-push-on-git-force-with-lease-fb15701218df) – Sabito stands with Ukraine Aug 21 '20 at 03:36
  • 1
    this is the only solution when you want to initially push a repository, but you get errors. should be done only once though. – Aris Nov 26 '20 at 14:35
  • Agree with Spaideri's comment (because this is not a normal process especially when working on a repo with others AND because warnings provided by others in this very comment list) but occasionally on a brand new repo init, this can be helpful. – Harlin Apr 12 '21 at 19:13
  • I get an error: **error: src refspec master does not match any error: failed to push some refs to** – IgorGanapolsky Apr 19 '21 at 19:58
  • Nothing wrong with using -f when initializing a newly created repository. Just make sure you know what you do when using -f on the existing repository. – Donovan P Jul 05 '21 at 01:51
  • To moderator: I got pinged by Stack Overflow and automatically assumed this was my answer. I do not know why that happened. Please ignore my comment and my flag. – Lasse V. Karlsen Jul 23 '21 at 20:34
  • True but dangerous – Miguel Gonzalez Sep 16 '21 at 15:00
  • 1
    it will remove all the old commits. – Usama Tahir Oct 05 '21 at 21:48
  • 1
    Sad, I lost my 162 commit by this. I don't know much about git as I only edit them from my browser, I tried git command line and this happen :( – Liso Dec 26 '21 at 05:05
  • I have no choice but force it because git cant handle the issue – Ishmael Mavor Raines Jun 10 '22 at 12:49
  • For those who are not pushing to master, just use: git push -f – Nube Colectiva Feb 18 '23 at 14:54
99

It happens when we are trying to push to remote repository but has created a new file on remote which has not been pulled yet, let say Readme. In that case as the error says

git rejects the update

as we have not taken updated remote in our local environment. So Take pull first from remote

git pull

It will update your local repository and add a new Readme file. Then Push updated changes to remote

git push origin master
Himanshu
  • 12,071
  • 7
  • 46
  • 61
77

git pull <remote> master:dev will fetch the remote/master branch and merge it into your local/dev branch.

git pull <remote> dev will fetch the remote/dev branch, and merge it into your current branch.

I think you said the conflicting commit is on remote/dev, so that is the branch you probably intended to fetch and merge.

In that case, you weren't actually merging the conflict into your local branch, which is sort of weird since you said you saw the incorrect code in your working copy. You might want to check what is going on in remote/master.

Jeff
  • 1,722
  • 13
  • 7
26

This usually happens when the repo contains some items that are not there locally. So in order to push our changes, in this case we need to integrate the remote changes and then push.

So create a pull from remote

git pull origin master

Then push changes to that remote

git push origin master
13

You can try this: git pull origin master --rebase

Eduardo Ramos
  • 131
  • 1
  • 2
  • 5
    Hi Eduardo! This worked for me. But can you explain why it works? What does this command do exactly? – Akshaya Natarajan Jun 11 '20 at 18:24
  • After add manually file README.md on site GitHub. I also can't push project from device to Git like normally. Your anwser helped me so much!!!! Thanks bro <3 – Nghien Nghien Feb 14 '22 at 18:43
11

You need to input:

git pull
git fetch 
git merge

If you use a git push origin master --force, you will have a big problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gaoyehua
  • 127
  • 1
  • 2
  • 10
    Why do you need to use `git fetch` and `git merge` again manually after running `git pull` which [contains them](https://stackoverflow.com/a/292359/711006)? – Melebius Apr 19 '18 at 09:58
10

Force to push

git push -f origin master

Samir Poudel
  • 195
  • 1
  • 9
9

I fixed it, but I'm not exactly sure what I did. I tried simply pushing and pulling using:

git pull <remote> dev instead of git pull <remote> master:dev

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thanos
  • 3,203
  • 4
  • 18
  • 27
9

This is how I solved this issue:

  1. git pull origin master
  2. git push origin master

This usually happens when your remote branch is not updated. And after this if you get an error like "Please enter a commit message" Refer to this ( For me xiaohu Wang answer worked :) )

Salomi Edward
  • 533
  • 4
  • 15
  • There isn't anyone with the name "xiaohu Wang" here, not even close (and not in deleted answers either). What answer does it refer to? – Peter Mortensen Jan 31 '23 at 20:22
7

git pull --rebase origin master

git push origin master


git push -f origin master

Warning git push -f origin master

  • forcefully pushes on existing repository and also delete previous repositories so if you don`t need previous versions than this might be helpful
Bhavesh Chand
  • 495
  • 6
  • 7
5

Well actually github is much simpler than we think and absolutely it happens whenever we try to push even after we explicitly inserted some files in our git repository so, in order to fix the issue simply try..

: git pull

and then..

: git push

Note: if you accidently stuck in vim editor after pulling your repository than don't worry just close vim editor and try push :)

deepchudasama
  • 480
  • 7
  • 18
5

I have done below steps. finally it's working fine.

Steps

1) git init

2) git status (for checking status)

3) git add . (add all the change file (.))

4) git commit -m "<pass your comment>"

5) git remote add origin "<pass your project clone url>"

6) git pull --allow-unrelated-histories "<pass your project clone url>" master

7) git push -u "<pass your project clone url>" master

Prabhat
  • 772
  • 1
  • 8
  • 22
4

The best option for me and it works and simple

git pull --rebase

then

git push

best of luck

lio
  • 419
  • 5
  • 9
3

You can use

git pull --rebase <your_repository_name> <your_branch>

This will help in case you have some changes not yet registered in your local repository, especially README.md.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
braspy
  • 45
  • 1
  • 7
2

I had this error and it was because there was an update on the server, but Sourcetree was not showing any updates available (possibly because I was offline when it last checked). So I did a refresh in source tree and now it shows two items to push instead of 1 item.

So be sure to press refresh or pull if you get this error and then try again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
2

If you have initialized a new GitHub repo with ReadMe file and also receiving a fatal error like this:

fatal: refusing to merge unrelated histories

then you probably want to give a try to the command below:

git pull origin master --allow-unrelated-histories

Now you can try pushing your project to your new repository:

git push origin [branch]

Note: If you have initialized repository in Github and also committed locally, then you need to use the suggested command above, the first command "git pull origin...". Otherwise you can simply type the following command:

git pull origin [branch]

Hope it was helpful.

Matin
  • 173
  • 2
  • 15
0

I had the same problem. It happened that I have created .Readme file on the repository without pulling it first.

You may want to delete .Readme file or pull it before pushing.

Martin Oputa
  • 349
  • 4
  • 6
  • I'm not sure this answer to a 5year old question provides any additional value, plus It does not provide a solution to the specific problem of the OP. Since you're a new contributor, please take a look at a guide on how to answer questions: https://stackoverflow.com/help/how-to-answer – Sotiris Panopoulos Mar 11 '20 at 20:14
  • Please explain further why adding a readme file should lead to the given error message – Nico Haase Oct 05 '20 at 15:34
0

The error possibly comes because of the different structure of the code that you are committing and that is present on GitHub. You may refer to: How to deal with "refusing to merge unrelated histories" error:

git pull --allow-unrelated-histories
git push -f origin master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kourosh Neyestani
  • 781
  • 2
  • 9
  • 16
  • 4
    Even if this surely will work, would you mind explaining it? Could there possibly be any danger in doing this? If yes, explain that even further, like: "Folks, be sure that you will loose commits using this" – Nico Haase Oct 05 '20 at 15:34
0

I had the same problem. Make sure that you are on the right Heroku account.

It appeared when I tried to push the changes to the wrong Heroku account.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nourza
  • 2,215
  • 2
  • 16
  • 42
0

1.git pull origin

2.accept the changes depending upon your requirement, like keeping incoming or keeping existing changes.

3.git add else give git add . 4.git commit -m "keep your commit message"

5.git push origin

This has worked to me. Let me know if it didn't work to you.

0

You could simply and safely revert the changes made by your colleague. using the git revert command.

git revert commit-hash

hash can be found with the following command.

git log

It's not ideal to revert a commit, in fact, it is a really rare case to do so. what happens often is that developers would work concurrently on similar files and do changes that are divergent. git is built in order to mitigate the amount of effort required to maintain a healthy codebase.

on top of git, services like Github, Atlassian, GitLab and possibly others provide handy interfaces and feature to reduce even more issues like that. Preventing direct push to a shared branch for example and replacing them by pull request using sub-branches instead.

If you're team lack a little organization. my suggestion for you is to work faster in order to push breaking changes before others. (jk)

Enough of my monologue, there are a few methods that I like to use when this problem occurs.

First, and obvious one, try to fix the conflict before pushing your changes. IDEs nowadays have really nice interface for conflicts this is an example on vs-code.

enter image description here

there are a couple of apparent buttons "Accept current, incoming, both" which will let you select which piece of code you want to keep, modify or remove. you can also edit the code manually if you wish! without the IDE consent of course.

This is great, for when your work is in progress and you actually need the changes made by your teammates. Or when you're done and you just want to land a PR before heading to bed after your third overtime.

you could do the same things via cmd, but you might end up bald by the end of the process.

If you're working on a large feature and you've done lots of changes but you want to keep your files uncommitted because it's handy sometimes to do so. stashing is your friend.

git stash > Will temporary store your changes locally until you do a 
git stash pop >  which will retrieve and apply the changes for you.

pulling a branch in between stash operations will not trigger conflict assuming your branch is close to the shared one. otherwise, you're back at step one.

My third and final little secret are diff files.

git checkout -b branch_a
git diff dev..branch_a > changes.diff
git apply changes.diff

What it basically does is that it compares the difference between two branches and creates a file called changes.diff it will show all the changes made by your colleague and will help you decide what to keep or remove

Btw! you can do a merge --abort to cancel m

Phil
  • 795
  • 1
  • 5
  • 15