ZL
About Articles Contact
Published on Nov 23, 2018
Filed under:
#video,
#git

Undoing changes in Git

Undoing with Git

At this point, you already know Git is like a save point system. What you’ve done so far is to learn to save. But how do you undo, and go back to a previous state?

That’s what we’re going to cover

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

  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

Scenario 1: Discarding local changes

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>
/* 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
Discard changes in the contextual menu

Scenario 2: Amending the previous commit

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

You can:

  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
The amend checkbox in the commit area

Scenario 3: Rolling back to a previous commit

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

You can:

  1. Go into the Git History
  2. Right click the commit you want to roll back to
  3. Select reset branch to here
Reset option in the contextual menu

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

Scenario 4: Reverting a commit that has been pushed to 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
Revert in the contextual menu
Commit the changes option is checked

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.

Previous Setting up my new Mac (Part 1—the apps I use) Next My CSS reset

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is the rare sort of developer who both knows his stuff and can explain even the most technical jargon in approachable — and even fun — ways!

I’ve taken his courses and always look forward to his writing because I know I’ll walk away with something that makes me a better front-ender.

Geoff Graham
Geoff Graham — Chief Editor @ CSS Tricks
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms