How to undo changes in Git

Zell Liew
Zell Liew
Dec 7, 2018 · 3 min read

You may already know that Git is like a save point system. What you generally learn with Git initially is to learn to save your changes and commit them to a remote repository. But how do you undo a change, and go back to a previous state?

That’s what we’re going to cover within this article.

I’ve covered the contents in this article in a video if you like learning by watching instead of reading.

Local vs Remote

It’s more complicated to undo something that’s already on the remote. This is why you want to keep things on your local until they’re kind of confirmed.

Four common scenarios

We’ll be covering the following four common scenarios

  1. Discarding local changes
  2. Amending the previous commit
  3. Rolling back to a previous commit
  4. Reverting a commit that has been pushed to the remote

Note: In the screen shots below, I’ve used the Fork for Mac OS Git Client. You can do the same in other similar Git clients.

The first scenario is when you’ve created some changes. They’re not committed yet. And you want to delete these changes.

Let’s say we want to create a new feature. We’re going to add some HTML and CSS into the project:

<!--In index.html-->
<div class="feature"></div>
```
```css
/* In CSS file */
.feature {
font-size: 2em;
/* Other styles */
}

To discard these changes:

  1. Go to the staging area
  2. Select the files where you want to discard changes
  3. Right click on the files
  4. Select discard changes
Image for post
Image for post

When you have created a commit and you missed out some changes and you want to add these changes in the previous commit message.

  1. Go to the staging area
  2. Stage the files to commit
  3. Click on the amend checkbox
  4. Edit your commit message
  5. Commit
Image for post
Image for post

You already have a few commits in your local repository. You decide that you don’t want these commits anymore and you want to “load” your files from a previous state.

  1. Go into the Git History
  2. Right click the commit you want to roll back to
  3. Select reset branch to here
Image for post
Image for post

Note: You can only reset to a commit that hasn’t been pushed into the remote.

If you have a commit that has been pushed into the remote branch, you need to revert it.

Reverting means undoing the changes by creating a new commit. If you added a line, this revert commit will remove the line. If you removed a line, this revert commit will add the line back.

To revert, you can:

  1. Go to the Git history
  2. Right click on the commit you want to revert
  3. Select revert commit
  4. Make sure commit the changes is checked.
  5. Click revert
Image for post
Image for post
Image for post
Image for post

Other scenarios

GitHub has a useful article that shows you how to undo almost everything with Git. It will be helpful if you face other scenarios. Read it here.

Thanks for reading. Did this article help you in any way? If it did, I hope you consider sharing it. You might help someone out. Thank you!

This article was originally posted at my blog.
Sign up for my newsletter if you want more articles to help you become a better frontend developer.

freeCodeCamp.org

This is no longer updated.

Zell Liew

Written by

Zell Liew

Frontend consultant; teaching frontend to self-taught devs at zellwk.com. Currently attempting to demystify Javascript at https://learnjavascript.today

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Zell Liew

Written by

Zell Liew

Frontend consultant; teaching frontend to self-taught devs at zellwk.com. Currently attempting to demystify Javascript at https://learnjavascript.today

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store