0Pricing
DevOps Bootcamp · Lesson

Undoing Changes Locally

Learn various methods to revert or reset local changes, including unstaging files and modifying previous commits.

Undoing Changes Locally is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Oops! Undoing Local Changes

Ever made a mistake or changed your mind while working on a project? Don't worry, it happens to everyone!

Git provides powerful tools to undo changes in your local repository. This lesson will show you how to safely revert unwanted modifications.

Always Start with `git status`

Before you undo anything, it's crucial to know the current state of your repository. The git status command is your best friend here.

It tells you which files are modified, staged (ready to be committed), or untracked.

git status

Unstage a Staged File

Sometimes you add a file to the staging area with git add, but then realize you don't want to include it in the next commit.

You can 'unstage' it, moving it back to the modified (unstaged) state, using git restore --staged <file>.

git add my_feature.js
git status
git restore --staged my_feature.js
git status

Discard Working Dir Changes (File)

What if you've modified a file in your working directory, but haven't staged it, and now you want to completely discard those changes?

The git restore <file> command will revert that file to its last committed state, effectively deleting your local modifications.

echo "new content" > index.html
git status
git restore index.html
git status

Discard All Uncommitted Changes

Be careful with this one! If you want to discard all changes in your working directory that haven't been committed yet, you can use git restore .

This will revert all modified files in your current directory and its subdirectories to their last committed state. Unstaged files will be lost!

echo "change 1" > file1.txt
echo "change 2" > file2.txt
git status
git restore .
git status

Amend the Last Commit Message

Made a typo in your last commit message? Forgot to mention an important detail?

You can amend (edit) the message of your very last commit using git commit --amend. This command opens your default text editor to modify the message.

git commit --amend -m "Updated commit message with fix"

Amend to Add Missing Files

Forgot to include a file in your last commit? No problem!

Stage the forgotten file(s) with git add, then use git commit --amend --no-edit. This adds the staged files to the previous commit without changing its message.

git add new_script.py
git commit --amend --no-edit

Why `git restore` is Preferred

You might encounter older guides suggesting git reset HEAD <file> for unstaging or git checkout -- <file> for discarding changes.

While these work, git restore is the newer, more intuitive command introduced to make these operations clearer and safer. It's designed specifically for undoing changes.

Putting it Together: A Scenario

Imagine you modify config.json, stage it, then realize you made a mistake and want to discard the change completely.

  • First, unstage it.
  • Then, restore it to its last committed version.
echo "bad config" > config.json
git add config.json
git status
git restore --staged config.json
git restore config.json
git status

Quick Check: Undoing

You've modified data.csv in your working directory, but you haven't staged these changes. You decide you want to completely discard these modifications and revert data.csv to its state from the last commit.

Recap: Master Undoing

Great job! You've learned how to safely undo local changes in Git:

  • Use git status to see your current changes.
  • git restore --staged <file> to unstage files.
  • git restore <file> to discard working directory changes for a file.
  • git restore . to discard all unstaged working directory changes (use with caution!).
  • git commit --amend to fix your last commit message or add missing files.

These commands are essential for maintaining a clean and accurate project history!

Frequently asked questions

Is the “Undoing Changes Locally” lesson free?

Yes — the full text of “Undoing Changes Locally” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Undoing Changes Locally”?

Learn various methods to revert or reset local changes, including unstaging files and modifying previous commits. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Undoing Changes Locally” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Staging, Committing, History
  2. Undoing Changes Locally
  3. Branching & Merging Basics
  4. Stashing Work in Progress
← Back to DevOps Bootcamp