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.
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
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:
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.
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.
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!