Undo a commit that has already been pushed to the remote repository

Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

If we pushed our changes already to the remote repository we have to pay attention to not change the git history (using commands like rebase, reset, amend etc). Other collaborators of the same repository might already have pulled your changes, thus resulting into horrible, strange merge conflicts if we change the git history. There are some “safer commands” for undoing in such cases.

Instructor: [00:00] Let's inspect the log history of the [inaudible] repository here. You can see here a couple of commits, and all of them have already been synced to the remote counterparts, so to the remote repository.

[00:11] If that situation happens and if we want to undo, for instance, that first commit, we have to pay attention to not apply git commands, which actually change the commit IDs in front of here, such as git reset or git amend and so on.

[00:27] Rather, what we have to do is to undo this in a safe manner. We can use the git revert command. We pick here the ID we want to revert, which could even be some commit further down here. We hit enter.

[00:40] It will then open up in editing mode and tell us, "What kind of commit message would you like to apply?" We can either leave it like this or change it. Let's leave it like this. We save it.

[00:51] If we log again, you can see that here a new commit has been created, which is the exact opposite of that commit here.

[00:59] Basically, if we added here a couple of lines, this commit will exactly remove those lines that have been added here. Since we have created a new commit message here, we can save to push it after the remote repository.

egghead
egghead
~ a minute ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today