1

I clone my own repository from github to local machine, do a small change, then git push it. The git log likes this:

3: xxxxx  
2: yyyyy 
1: zzzzz 

I suddenly find there is an error in commit 3, so in local repository, I do git reset 2 first, modify the file again, then git commit it. So the git log changes to this:

4: ttttt
2: yyyyy
1: zzzzz

This time when I git push origin, the git complains:

To https://github.com/NanXiao/torch.git
! [rejected]        patch-1 -> patch-1 (non-fast-forward)
error: failed to push some refs to 'https://github.com/NanXiao/torch.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.

So how can I revert the remote repository? Make the remote repository's git log like this:

4: ttttt
2: yyyyy
1: zzzzz
Nan Xiao
  • 16,671
  • 18
  • 103
  • 164

1 Answers1

2

To recap what you actually did to your branch, you started out with this:

3: xxxxx
2: yyyyy
1: zzzzz

You decided there was a problem with commit 3 so you nuked it using git reset --hard HEAD~1, leaving you with this:

2: yyyyy
1: zzzzz

Then, you made a new commit on top of this branch:

4: ttttt
2: yyyyy
1: zzzzz

The non-fast-forward error you saw makes sense since Git has no way of simply adding commit 4 on top of the remote branch which either still ends in commit 3 or ends in another commit which someone else made on top of commit 3.

That being said, if you want to force your current branch to the repository, you can do so via:

git push origin your_branch --force

Take heed when doing a force push because it can cause problems for anyone else who is currently working with your branch.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360