There are two repositories:

  1. bare repository, everyone takes a copy from it, it has a master branch, which always corresponds to the production state
  2. there is a local repository

The developer rolls back to the previous commit in the local git reset --soft , then makes a commit, then push.

The push is rejected, the developer makes a pool and merges the local master with a remote one, after which the canceled lines of code again fall into place and the meaning of the idea is lost!

Tell me, please, how to roll back to the commit correctly and upload it to the remote repository?

  • 2
    Take advantage of git revert and roll back any extra commits. - Costantino Rupert
  • 2
    As an option git push --force . Just do not abuse. - a_gura
  • one
    ! [] ( i.imgur.com/FpAdsW1.jpg?1 ) - Costantino Rupert
  • Cat, everything is fine, but here's the situation, merge occurred, after that one commit, trying to roll back the last change to the state before merge, writes that you need git revert -m I tried this and that, but something really did not work out. .. Maybe there was such an experience? Thanks for the answers again! =) - Costa
  • Wait, did you make a local merge, then some kind of commit from above and want to cancel this local commit? Then use git reset --hard HEAD~1 . I recommend immediately understanding how the rest options of the reset command work (that is, --soft and the default option --mixed ). - Costantino Rupert

2 answers 2

For simplicity, assume that there is only one problem commit.

To rollback a published (push) commit, there are two main ways:

  1. git revert номер_проблемного_коммита . Creates a second, "opposite" commit, with a minus sign. After it is published, you will get a state like before the problem commit, but in the history there will be a pair of unnecessary-commit + cancel-unnecessary-commit.
  2. git push --force . Before this, you need a git reset , as advised in the comments. Use this option with caution. Consider the following cases:
    1. No one has yet seen the published changes (including the "robots" that can do something automatically when pushing). Then --force best option.
    2. Several people have already been updated after an unsuccessful commit. Then before using --force should notify them, because on their side --force can "stir up" the situation and require additional actions. If, instead of placing another problematic commit, there is no other commit yet, then the problematic commit can again get to the server (even without --force 'a) from these developers.
    3. Someone has already posted changes on top of a failed commit. Then --force wipe their new commits. You can protect against this with the option --force-with-lease . In this case, you need to upgrade and do git rebase -i , and then --force (and more new commits can happen during this operation). Here git revert will be more appropriate.
    4. Many people have already been updated, there are several published or unpublished changes "on top" of a bad commit: git revert .

as an option

git reflog - will display a list of HEAD with numbers and description, it is enough to select the state of interest to you and make a reset to this HEAD.

git reset --hard HEAD @{ number }

HEAD is a pointer to the current branch, which, in turn, is a pointer to the last commit made in that branch. This means that HEAD will be the parent of the next created commit. As a rule, the easiest thing to consider is HEAD as a snapshot of your last commit.