-1

Following How can I rollback a github repository to a specific commit? I did

git reset --hard <my-commit-id>

now when I do git status it shows: your branch is behind 'origin' by 8 commits....

How can I push this previous version of commit to my master therefore it becomes a master.

When I try to push it tells me

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/hel.github.io.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.
kowsky
  • 12,647
  • 2
  • 28
  • 41
chaikov
  • 495
  • 4
  • 18
  • Is it a one-person repo? (or at least a one-person branch) or do you share it with people? – Romain Valeri Dec 09 '19 at 09:02
  • for myself only. – chaikov Dec 09 '19 at 09:04
  • 1
    Note that what the server is telling you is a kind of long winded way of saying "if I do what you ask, eight commits will be lost, probably forever". But that's what you want: you *want* the server to lose those eight commits, forever. That's why a force option (preferably "with lease" to make sure you're right about which eight commits they'll lose forever) is appropriate. – torek Dec 09 '19 at 10:28
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+tip+of+your+current+branch+is+behind – phd Dec 09 '19 at 10:33

1 Answers1

3

In general it is not recommended to rewrite history, especially not on master branch, because if other people work on the same repository, then the references become invalid.

In this case however, since you say you are the only one working on this repository, you can forcibly rewrite history; therefore you should use:

git push --force-with-lease

NOTES

  • git push --force-with-lease does almost the same as git push --force , but it is more secure if you work with others...
  • git push --force-with-lease or git push --force are dangerous commands. the force in their name indicates that you will rewrite history, so always use with care.
Chris Maes
  • 35,025
  • 12
  • 111
  • 136