Skip to main content
X

Explore your training options in 10 minutes

Git Undo Add: A Guide

James Gallagher - December 29, 2020


There’s plenty of reasons why you would want to remove an item from a Git commit. If you’ve not finished working on a file, it may not be ready to commit; you may still be testing it, or you may want to push your changes to that file as part of another commit.

Whatever the reason, you’ll maybe be wondering: how do I undo a git add? That’s a great question. In this short guide we’re going to walk you through undoing a git add using the git reset command. Let’s begin!

The Architecture of a Project

Before you can undo a git add, you’ve got to learn about how Git projects are structured.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

Your local version of a project is stored in your working directory. This is your copy: you can change it as much as you would like. Changes you make to your local version of a repository are not reflected in the main version of a codebase until you commit them.

Once you’re ready to add a change to a commit, you need to move it to the staging area. This is where you store all the files that you want to add to your next commit. When you have added all the files that you want to add into a commit, you can create a commit using git commit .

Adding items to the staging area is a reversible process. The changes you push to the staging area are not reflected in the main version of a repository until you commit them.

You can learn more about the git add file command line operation in our git add tutorial.

How to Undo a Git Add

Once a file has been committed, use git reset to remove it from a commit. This command removes a file from the staging area without making any changes to the file.

Let’s remove a file called README.md from a commit:

git reset README.md

Upon running this command, README.md is removed from the staging area. When you view the file you have removed from the staging area, you will notice no changes have been made. This is because the git reset command does not alter the contents of a file.

This command also works to remove all the files from a commit. Let’s say that you have just added every file in a repository to a Git commit and you didn’t mean to. You could reverse this using the following command:

git reset

When you use reset with no filenames, it will remove every item from the staging area.

The git reset command does not revert your repository to older versions or previous commits.

git rm: A Warning

The git rm command allows you to remove files from the staging area. This command has a dual purpose: it removes items from your local copy of a repository.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

You should only use the git rm command if you want to remove items from both the staging area and your working directory. To remove a file from the staging area and your local copy of a project, you can use git rm like this:

git rm README.md

This has removed README.md from the staging area. When we go to check the contents of this file, the file no longer exists. It’s been deleted from our system.

Conclusion

The git reset command allows you to remove a file or multiple files from a Git commit. If you want to remove an item from both the staging area and your local copy of a repository, you can use git rm. Use git rm with caution.

Now you’re ready to start undoing commits using Git commands like a professional!

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.

Skip to main content