68

I know people have asked similar questions, but I believe the causes of their problems to be different. I did a hard reset because I had messed up my code pretty bad

 git reset --hard 41651df8fc9

I've made quite some changes, I've made some commits and now that I'm trying to push all these commits into the server I get the following error:

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@git.somewhere.git'

Git suggests to do a git pull and that's what other people have suggested to other users. However, I believe that a git pull will merge my current code with the code that I don't want anymore (head revision). How can I do a push and forget about the version/revisions ahead of me?

Eric
  • 3,301
  • 4
  • 33
  • 39

8 Answers8

106

git push -f if you have permission, but that will screw up anyone else who pulls from that repo, so be careful.

If that is denied, and you have access to the server, as canzar says below, you can allow this on the server with

git config receive.denyNonFastForwards false
blueshift
  • 6,742
  • 2
  • 39
  • 63
  • I guess I don't have permission 'remote: error: denying non-fast-forward refs/heads/master (you should pull first)' I'm the only one working on this repo at the moment, so I'm not worried about any other branches or anything. Any ideas? – Eric Mar 23 '12 at 00:12
  • If you are the only person own this repo, just use `git push -f`, which will use your current repo replace the remote one. If there are multi users development, fast-forward is essential, otherwise, it will very easy happen distaste . – Tim Mar 23 '12 at 01:24
  • 1
    If you can log in on the remote, you can go right into the bare git repo and manually rewind the branch, with `git branch -f`, e.g., `git branch -f rewind_the_one_I_broke 8120307` for example. You can run `git log` in a bare repo to find the reset-point. Note that this has the same effect as a `git push -f` but bypasses the hooks. – torek Mar 23 '12 at 05:57
  • 3
    If you have access to the bare repository, you can temporarily update your repository configuration with `git config receive.denyNonFastForwards false`. I found mine was set to `true` by default. – canzar Jul 10 '14 at 19:33
  • @canzar the `git config receive.denyNonFastForwards false` is required for some of my `git push --force` commands to work. Thanks for the reference. – Johnny Utahh Jul 06 '15 at 18:17
  • I had to do this to revert to a list commit e.g. `git revert --hard 010d3....,` then call `git push origin branch -f` – cwiggo Oct 08 '21 at 14:34
32

If you are the only the person working on the project, what you can do is:

 git checkout master
 git push origin +HEAD

This will set the tip of origin/master to the same commit as master (and so delete the commits between 41651df and origin/master)

Seanny123
  • 8,776
  • 13
  • 68
  • 124
ouah
  • 142,963
  • 15
  • 272
  • 331
  • will this get rid of the code that I don't want anymore and keep my new code? (sorry if it's a dumb answer) – Eric Mar 23 '12 at 01:06
  • 2
    this will set the tip of origin/master to the same commit as master (and so delete the commits between 41651df and origin/master) – ouah Mar 23 '12 at 01:26
  • Update the origin repository’s master branch with the your current HEAD located branch, allowing non-fast-forward updates. So, this is the same with `git push HEAD -f`. For me, I think, you can use a more gentle way to do this, first, use `git fetch`, after that, use `git rebase -i origin/master`, this will let you select the commits. – Tim Mar 23 '12 at 01:39
  • 7
    AHHHH. SHOULD HAVE READ THE COMMENTS BEFORE RUNNING THE COMMAND. – Seanny123 Dec 14 '13 at 08:00
  • NOT WORKING DEAR, THANKS A LOT. – Kamlesh Mar 27 '23 at 13:06
21

Just do

git pull origin [branch]

and then you should be able to push.

If you have commits on your own and didn't push it the branch yet, try

git pull --rebase origin [branch]

and then you should be able to push.

Read more about handling branches with Git.

Robin Wieruch
  • 14,900
  • 10
  • 82
  • 107
4

'remote: error: denying non-fast-forward refs/heads/master (you should pull first)'

That message suggests that there is a hook on the server that is rejecting fast forward pushes. Yes, it is usually not recommended and is a good guard, but since you are the only person using it and you want to do the force push, contact the administrator of the repo to allow to do the non-fastforward push by temporarily removing the hook or giving you the permission in the hook to do so.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 1
    Or, have the admin run `git branch -f`, which has the same effect but does not require fussing about with the pre-receive hook. – torek Mar 23 '12 at 06:00
3

What I did to solve the problem was:

git pull origin [branch]
git push origin [branch]

Also make sure that you are pointing to the right branch by running:

git remote set-url origin [url]
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
3

for me following worked, just ran these command one by one

git pull -r origin master

git push -f origin your_branch

Community
  • 1
  • 1
Sultan Ali
  • 2,497
  • 28
  • 25
  • this solved my issue. The main issue I had was that I changed the default branch name manually then things started falling apart. These commands solved my issue. thanks! – ASH Jul 25 '21 at 05:20
0

I did the following steps to resolve the issue. On the branch which was giving me the error:

  1. git pull origin [branch-name]<current branch>
  2. After pulling, got some merge issues, solved them, pushed the changes to the same branch.
  3. Created the Pull request with the pushed branch... tada, My changes were reflecting, all of them.
karel
  • 5,489
  • 46
  • 45
  • 50
Aniket J kamble
  • 130
  • 1
  • 8
0

What worked for me after was to fetch from all remotes before pushing:

git fetch --all
git push

Before that I was doing git pull (ok) and then git push but the issue persisted until fetching from all remotes first.

Maybe my branches were related to each other.

I hope this solves your problem.