The git revert
command is also used to undo changes made to a repository. However, it does not remove these changes from Git history. It inverts the changes made after the specified commit. Then, it creates a new commit with the resulting changes.
Key takeaways:
The staging area (or index) is where changes are prepared before committing, separating modified files from the repository.
The reason for the “undo” is that we may accidentally stage files that are not yet ready for commit.
Unstaging methods:
git reset <filename>
: Unstage a file
git reset
: Unstage all files
git restore --staged <filename>
: Unstage a specific file while keeping changes
git restore --staged .
: Unstage all files
When editing code in Git, we sometimes commit changes that we shouldn’t. The great thing is that Git allows us to fix this effortlessly. In this Answer, we’ll discuss different commands that can be applied to the unstaged files present in our git
repository, as well as explain what happens to your changes when you use these commands.
Before diving into how to undo git add
, it’s essential to understand the staging area (also known as the index) in Git:
Working directory: This is where we’ll make changes to files.
Staging area: This is where we prepare changes to be committed. When we run git add
, we move changes from the working directory to the staging area.
// to stage a filegit add filename// to stage all changed filesgit add .
Repository: This is where your committed changes are stored. After you run git commit
, the changes in the staging area are saved to the repository.
git add
Sometimes, we may move changes to the staging area unintentionally and will need to undo this operation. For this, we can use the following two methods:
Both of these methods unstage the files from the staging area.
git reset
commandgit reset filename
git reset
When calling reset
, the changes made to the files before executing git add
will be available.
git restore --staged
git restore --staged filename
The following command is used to unstage all changes in the staging area while keeping the modifications intact in your working directory:
git restore --staged
Hands-on practice is essential for truly understanding and mastering new concepts. Just as Donald Knuth suggested:
“The only way to learn about new things is to do them; the way to learn usability is to make something usable.”
Let’s run the following commands in the terminal given below to first add files to the git
local repository and then reset them from the staging area:
We will first create three files with some text, such as README.md
, first.html
, and last.html
.
To add files for Git to track, use the git add filename
command.
Next, run the git reset
command to remove a file from git add
.
cd gitgit initecho "# My README.md file" > README.mdecho "# My second file" > first.htmlecho "# My third file" > last.htmlgit add .git ls-filesgit reset filenamegit ls-files
Let’s execute the above commands one by one and see how to undo git add
:
Haven’t found what you were looking for? Contact Us
Free Resources