Home Software Engineering How To Unstage Files on Git

How To Unstage Files on Git

by schkn

When working with Git, it is quite common for developers to add all the files to your index in order to prepare them for commit.

However, in some cases, you may want to remove files from the index, in other words, you want to unstage files.

Unstaging files is very beneficial : it can be used to separate files in different commits, or to do work on some other modifications.

In this tutorial, we are going to see how you can easily unstage your files on Git.

Unstage files using git reset

The easiest way to unstage files on Git is to use 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.

So what does this 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 it 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, 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 commit 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 softly

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 hardly

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 learnt how you can unstage files easily on Git using the “git reset” command.

You learnt 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 and software engineering, we have a complete section dedicated to it on the website, make sure to have a look!

Unstage files on Git using git reset

You may also like

4 comments

Kirill Pletnev November 25, 2019 - 11:25 am

Thank you for the awesome posts!

Want to clarify one moment: I cannot add a file to Unstaged state, `git reset — README` moves it to the Untracked state.

Reply
schkn November 25, 2019 - 4:48 pm

Thank you!

Was the file tracked before?
Because if you add a file to the index that was not indexed before, it will go to the untracked state when trying to unstage it.

Reply
How To Remove Files From Git Commit – devconnected December 14, 2019 - 11:28 am

[…] that your files are in the staging area, you can remove them (or unstage them) using the “git reset” command […]

Reply
Anthony January 16, 2023 - 12:02 pm

Not sure when this post was made but `git reset HEAD~ –soft` only removes the lastest commit and put files in staged status. You need to use `git restore –staged ` to unstage files.

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.