Back repository to previous state

Asked

Viewed 3,088 times

3

I have a project that we use GIT for versioning. However, one of the programmers uploaded a wrong version to the remote server, gave commit, push and several commits and pushs.

Then the site gave error. We tried git checkout <commit numero>, git revert and nothing!

I want to undo all the changes he made on a certain date to what it was before, and understand how to undo those changes after a user has given a push on the server.

1 answer

4

First, give a git log to recover the hash of all the last commits. For example:

git log -n 20 # lista os últimos 20 commits

In this list, copy the hash from the last stable commit. Then, take a git rebase to rewrite commit history:

git rebase -i <hash do último commit estável>

(attention: this command literally rewrites the commit history, so be careful!)

Then you will enter a screen that lists all commits between the HEAD and the hash you chose. Discard the wrong commits simply by deleting the line referring to commits.

Finally, just give one git push -f to move the history rewrite to the remote repository.

On the server, just give one git pull --rebase to fetch the latest repository changes.

  • It is highly inadvisable to delete commits that have already been published. Ideally, reverse them, not delete them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.