How to perform git undo add PROPERLY [5 Examples]

If you have a hard time undoing changes in the staging area, worry no more. All you need is to implement the tricks you will get in this tutorial.

Use any of the following commands to git undo add on uncommitted files:

Advertisement
git rm --cached <file>
git reset -- <file>
git reset <file>

Use any of these commands to apply git undo add on modified files you have committed:

git restore --staged <file>
git reset HEAD <file>
git reset .

Before taking a practical look at the commands using both local and remote repos, it would help understand terms that often confuse developers trying to understand git undo add.

 

10 terms to understand before attempting git undo add

Here are the often confusing terms that make it hectic to achieve git undo add effortlessly. They affect the git workflow in the working directory, index, and history.

  1. Repo: A directory hosting tracked files. Files tracked on your machine (often offline) are said to be in a local repo. Once the files are online on a website, such as Github, they are on the remote repo.
  2. Git init: Instantiate a repo.
  3. Working directory: A place to actively change files.
  4. Git add: Command git to take a snapshot of the file changes in the index.
  5. Index: A temporary file store, holding the changes before saving them in the git database. The index is also called the staging area. It is where you target when doing git undo add.
  6. History: Once the files are in the git database, they are part of commit history.
  7. Commit: Save files in the git database with a unique hash.
  8. Git status: Enquire the stage in the git workflow that your changes have reached.
  9. HEAD: The commit reference you are or viewing, which in most cases is the latest commit in the history.
  10. Push: Synchronize local and remote repos by updating remote with local repo's changes. The opposite of git push is git pull.

Now that you are comfortable with the terms we will use in this tutorial, let us see the practical implementation of git undo add.

 

Lab setup to explore git undo add

We will work with both local and remote repos to practice git undo add before and after modifying files.

I am creating a remote repo called git_undo_add on Github.

Advertisement

How to perform git undo add PROPERLY [5 Examples]

Copy the clone URL and head over to the command line or terminal and clone the repo locally.

How to perform git undo add PROPERLY [5 Examples]

Let us divide the workstation into two: git undo add for new files and git undo add for modified files.

In the context of this tutorial, a new file is accessing the staging area for the first time. On the other hand, a modified file has been through the staging area to the commit stage and probably pushed, as you are about to see.

Let us get started with new files.

 

Examples to show how to undo add on “new” files in git

Example-1: Using the cached command

Navigate to the repo we cloned.

cd git_undo_add

Check if your repo is instantiated using

git status

If not, run the command

git init

before proceeding with the tutorial.

Advertisement

Create a file.

touch file

Check status.

git status

We have one untracked file.

How to perform git undo add PROPERLY [5 Examples]

Stage the file using any of these commands:

git add file

OR

git add .

OR

git add *

OR

git stage file

Check the status.

Advertisement
git status

The message changed from red to green. That means git temporarily holds the files at the index while waiting for our next order. Simply put, we have cached the file.

git status

How to perform git undo add PROPERLY [5 Examples]

Let us git undo add in the staged file using the command

git rm --cached file

Here is a breakdown of the command. rm is the short form of remove and the --cached flag references a staged file.

How to perform git undo add PROPERLY [5 Examples]

Recheck the status.

git status

How to perform git undo add PROPERLY [5 Examples]

Voila, the file is back to the untracked stage!

 

Example-2: Using git reset command

There are two ways to git undo add using the reset command. You can either use double dashes or ignore them. Let us start with the double dash way.

Restage the file.

git stage file

untracked files after git undo add

Run this command

git reset -- file

on your command line or terminal by specifying the file to unstage.

Recheck the status.

git status

The files got untracked again.

Ignoring the double dash achieves a similar result.

Let us stage the files again

git add .

then run the reset command

git reset file

and recheck the status

git status

The files are back to the working directory thanks to git undo add using the reset command.

Here is what happens under the hood.

Git reset exists in three forms: git reset hard, git reset soft and git reset mixed:

git reset --hard <file>

git reset --soft <file>

git reset --mixed <file>

Git reset hard undoes the changes from the commit history and deletes the files from the staging or working directory. Git reset soft changes the HEAD's reference commit by keeping the changes in the staging area.

Lastly, git reset mixed is the default git reset form. That is why it is possible to use it without --mixed flag. In this tutorial's context, we use it to git undo add changes from the index because it undoes changes from the staging area to the working directory.

 

Examples to show how to undo add on “modified” files in git

Apart from undoing changes on files before committing them, you may want to undo git undo add on subsequent changes in a committed file. In this section, we will stage, commit and push files, then undo the changes using three commands, git restore, git reset HEAD and git reset all.

Before unstaging the files, clear the untracked file using the command

rm file

Only README.md exists in the working directory.

How to perform git undo add PROPERLY [5 Examples]

Let us create a text file.

touch file.txt

Stage the file,

git add file.txt

commit the changes

git commit -m "track text file"

and push them.

git push

Running the command

git status

shows the two repos are in sync.

How to perform git undo add PROPERLY [5 Examples]

Assume we append some files to file.txt

echo here >> file.txt

then stage the files.

git add .

As we are about to commit the changes, we realize we staged the wrong changes and need to git undo add on the modified file. You can use any of these three commands.

 

Example-3: Git undo add using the restore command

Running

git restore --staged file.txt

command on the command line removes the changes from the index, and we can confirm that by replaying the command

git status

How to perform git undo add PROPERLY [5 Examples]

 

Example-4: Git undo add on modified files by resetting the HEAD

Let us modify file.txt and stage it.

echo git undo > file.txt
git add file.txt

Then, undo the add action on the modified file.

git reset HEAD file.txt

How to perform git undo add PROPERLY [5 Examples]

What if we want git undo add on multiple modified files? That is where the git reset all comes in.

 

Example-5: Git undo add on modified files using git reset all command

Let us create, add and commit a second text file.

touch file2.txt

git add file2.txt

git commit -m "track file2"

Assume we modify both committed files, as follows

echo edit 1 >> file.txt

echo edit 2 >> file2.txt

and stage them.

git add .

We can use

git reset .

to git undo add on the files.

git status

confirms the files got unstaged.

How to perform git undo add PROPERLY [5 Examples]

Although the cached and reset commands can also undo modified committed files, the most suitable commands to use are restore, reset HEAD, or reset all.

 

Conclusion

The six typical commands to simplify git undo add are

  • git rm --cached <file>
  • git reset -- <file>
  • git reset <file>
  • git restore --staged <file>
  • git reset HEAD <file>
  • git reset .
  • git rm --cached <file>
  • git reset -- <file>
  • git reset <file>

which can undo new or modified files, whereas

  • git restore --staged <file>
  • git reset HEAD <file>
  • git reset .

are specific to modified files after git commit.

It is your turn to apply the most suitable command according to the challenge you want to solve.

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X