How To Unstage Files On Git?

While using Git there are different phases for a commit. Developers add all files into an index for commit preparation. Sometimes we may need to remove files from the index which is called as unstage. The upstaging files are useful to prevent the part of the next commit. In this tutorial, we examine different ways and methods in order to remove files from committing by upstaging them.

Unstage Files with “git reset” Command

The git reset command is the easiest way in order to unstage files by removing them from the index. The git reset command syntax is like below.

git reset -- FILE
  • FILE will be unstaged.

Before unstaging a file from the Git repository we can use the git stat command in order to list current indexed files.

$ git status
Git Status

From the output, we can see that two new files are indexed. These files are “help.c” and “scan.c”. We can unstage them with the git reset command like below. We provide the file name we want to unstage as the parameter.

$ git reset -- help.c

Also, we can unstage multiple indexed files by using the “git reset” command.

$ git reset -- help.c scan.c

We can use “git status” to see the current case. We can see that the “help.c” and “scan.c” are unstaged and colored red.

Git Unstaged Files

Unstage All Files with “git reset” Command

Previously we unstaged files one by one or multiple of them. The “git reset” command can also unstage all indexed files with a single command. There is no need to specify file names to unstage them. By running “git reset” all files are unstaged automatically.

$ git reset

Remove Unstaged Files

By default, the unstaging a file only removes it from the git index but does not remove or delete it completely. In some cases, we may need to remove or delete unstaged files completely. The git checkout command can be used to remove unstaged files. The syntax of the “git checkout” is like below.

git checkout -- FILE

In the following example, we remove or delete the file “scan.c”

$ git checkout -- scan.c

Alternatively, we can remove multiple files with the “git checkout” command.

$ git checkout -- scan.c help.c

Unstage Commits Softly

Sometimes we may need to unstage already committed files. The git reset command can be used to unstage files which are already committed. The commit unstages committed files into the current working directory which is called as unstage commit softly . The --soft parameter is provided to the “git reset” command.

$ git reset --soft 68342f2792dff75d1c6

Unstage Commits Hardly

An alternative way to unstage commits is unstaging the commit hardly. The hard way removes all files which are not stored in the current working directory and can not be recovered. The --hard parameter is provided with the commit ID.

$ git reset --soft 68342f2792dff75d1c6

Leave a Comment