As developers, using & working with Git is mandatory. Git Users every time deals with many files in the local repository. If any user accidentally added any file to your Git then you can remove it from your index by performing the unstage files on Git. Unstaging file is very beneficial, it can be used to separate files in different commits or to do work on some other modifications.

If you are a beginner, then take help from the Junos Notes provided Git Commands Tutorial. However, this tutorial is completely on How to unstage files on Git and it addresses various methods to unstage all files or particular files or committed files elaborately in the below modules.

Prerequisites

  • Firstly, Installation of Git is the required
  • A Git project
  • Create a local and remote repository
  • A terminal window/command line
    • Linux: Activities > Search > Terminal
    • Windows: right-click Start > Command prompt (or Windows PowerShell)

Unstage a File in Git

In Git, unstaging a file can be done in two ways.

1) git rm –cached <file-name>
2) git reset Head <file-name>

1. Unstage Files using git `rm` command

One of the methods to unstage git files is using the ‘rm’ command. It can be used in two ways:

1) On the brand new file which is not on Github.
2) On the existing file which exists on Github.

Let’s check the process of using the git rm command in both scenarios.

Case 1: rm –cached on new file which is not committed.

rm –cached <brand-new-file-name> is useful to remove only the file(s) from the staging area where this file is not available on GitHub ever. After executing this command, the file rests in the local machine, it just unstaged from the staging area.

Example:

$ git add filetwo.txt
$ git status

On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: filetwo.txt

$ git rm --cached filetwo.txt
$ git status

On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
filetwo.txt
nothing added to commit but untracked files present (use "git add" to track)

Case 2: rm –cached on existing file.

If ‘rm –cached <existing-file-name>.’command is utilized on the existing file on git then this file will be considered for delete and endures as untracked on the machine. If we make a commit after this command then the file on Github will be deleted forever. We should be very careful while using this command. So, this case is not advised for unstaging a file.

Do Refer: How To Clean Up Git Branches

2. Unstage Files using git reset

The most effortless way to unstage files on Git is by using the “git reset” command and specify the file you want to unstage.

git reset <commit> -- <path>

By default, the commit parameter is optional: if you don’t specify it, it will be referring to HEAD.

What does the git reset command do?

This command will reset the index entries (the ones you added to your staging area) to their state at the specified commit (or HEAD if you didn’t specify any commits).

Also, we use the double dashes as argument disambiguation meaning that the argument that you are specifying may be related to two distinct objects: branches and directories for example.

As a quick example, let’s pretend that you added a file named “README” to your staging area, but now you want to unstage this file.

$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   README

In order to unstage the README file, you would execute the following command

$ git reset -- README

You can now check the status of your working directory again with “git status”

$ git status

On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        README

nothing added to commit but untracked files present (use "git add" to track)

As you probably understood by now, the “git reset” command is doing the exact opposite of the “git add” command: it removes files from the index.

Unstage all files on Git

Previously, we have seen how you can unstage a file by specifying a path or a file to be reset.

In some cases, you may want to unstage all your files from your index.

To unstage all files also you can use the “git reset” command without specifying any files or paths.

$ git reset

Again, let’s pretend that you have created two files and one directory and that you added them to your staging area.

$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   README
        new file:   directory/file

In order to unstage all files and directories, execute “git reset” and they will be removed from the staging area back to your working directory.

$ git reset 

$ git status

On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        README
        directory/

nothing added to commit but untracked files present (use "git add" to track)

Remove unstaged changes on Git

In some cases, after unstaging files from your staging area, you may want to remove them completely.

In order to remove unstaged changes, use the “git checkout” command and specify the paths to be removed.

$ git checkout -- <path>

Again, let’s say that you have one file that is currently unstaged in your working directory.

$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README

no changes added to commit (use "git add" and/or "git commit -a")

In order to discard changes done to this unstaged file, execute the “git checkout” command and specify the filename.

$ git checkout -- README

$ git status

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Alternatively, if you want to discard your entire working directory, head back to the root of your project and execute the following command.

$ git checkout -- .

$ git status

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Unstage Committed Files on Git

In some cases, you actually committed files to your git directory (or repository) but you want to unstage them in order to make some modifications to your commit.

Luckily for you, there’s also a command for that.

Unstage Commits Soft

To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash.

$ git reset --soft <commit>

Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily.

$ git reset --soft HEAD~1

Using the “–soft” argument, changes are kept in your working directory and index.

As a consequence, your modifications are kept, they are just not in the Git repository anymore.

Inspecting your repository after a soft reset would give you the following output, given that you unstaged the last commit.

$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README

What happens if you were to hard reset your commit?

In this case, all changes would be discarded and you would lose your changes.

Unstage Commits Hard

To unstage commits on Git and discard all changes, use the “git reset” command with the “–hard” argument.

$ git reset --hard <commit>

Note: Be careful when using the reset hard command, you will lose all your changes when hard resetting.

Conclusion

In this tutorial, you learned how you can unstage files easily on Git using the “git reset” command.

You learned that you can either specify a path or a single file to unstage files and keep on working on your files until you commit them to your repository.

If you are curious about Git, we have a whole section dedicated to it on the website, make sure to have a look!

Leave a Reply

Your email address will not be published.