How to rollback push history?

Hi
Github is very great and excellent for source management.
While using github, I’ve faced some problems about rollback.

Several commits are already pushed to Github but unfortunately some errors are occured and I had to rollback source back.

But I don’t know how to rollback pushed commits.

Does anyone help me how to rollback pushed history on server?

Thank you.

Do you want to revert commits (which means to undo their changes with a new commit), or actually delete some of the history (basically go back to an old commit and continue from there)?

Both are possible, but note that editing the history will likely cause trouble if you already shared it beyond a feature branch.

I need both sides. Could you explain more details?

git revert can create (or prepare) commits that revert changes made in previous commits. So you get new commit(s) that undo changes of previous ones.

A manual approach would be to use git checkout to get the relevant files as they were at the commit you want, and then commit as usual.

If you don’t care about keeping the history consistent, you can:

  • Check out a previous commit in a new branch, continue from there (possibly rename/delete branches later).
  • git reset a branch to a previous commit. Caution, that might overwrite data (especially with --hard). Double check the command!
  • And finally there’s the option to rewrite history as you like with git rebase. That’s the most powerful and most complicated option.

Again, changing history can cause trouble if you already shared your work. People generally expect at least the default branch of a repository to stay consistent, feature branches and other WIP are commonly fine to rebase (all depending on how the project is organized). :warning:

2 Likes

I appreciate your help! :grinning: