How to Perform Git Unstage for Files

Photo of author

By admin

Git uses indexing to allow the developers to decide which codes they should include in the commit. For beginners, you can change the files using the `git add` and when you make a commit to the files, you will see the changes are automatically added. But when you mistakenly do improper indexing, and you don’t know how to bypass those changes completely, you can’t push them to the repo. But you need to do something with these changes so that they don’t go completely to waste. That’s where unstage comes in that refers to removing the changes of the files from staging. Let’s learn about indexing and unstaging files in Git from this post in a jiffy.

What is Index in Git?

In Git, we will find three stages of indexing. You need to create or change your files in a directory as you would do with the files that are on your hard drive. Then you should mention which changes you want to combine together. After that, take the changes, set them in a position, and describe them. And finally, you will create a commit that you can store in Git for future purposes. Indexing files are important because they help you to combine the changes together. When you are making changes to the commit, you need to pick the changes effectively. Index refers to a file stored in .git/index and when you create a commit, this file is reset to an empty state, and when you add more changes, those are added to the file. If you want to learn how to unstage the changes, you should get familiar with the commands `git reset` and `git rm`. Let’s talk about the ways you can unstage files on Git.

Unstaging Files on Git

Use the “git reset” command and mention the files that you should unstage from Git:

git reset <commit> — <path>

If you don’t designate the files, the parameter will go to HEAD. But the above command shall reset the index entries or the specifics that you have put in the staging area. You can add double dashes as argument disambiguation which means that the argument you are specifying has two different objects such as branches and directories. For instance, you have added a file that we call “README” to the staging and to unstage it, you will have to use the following command:

$ git status

On branch master

Your branch is up to date with ‘origin/master’.

Corrections to be performed:

(use “git reset HEAD <file>…” to unstage)

new file: README

To unstage the README file, you need to practice the instruction below:

$ git reset — README

Then check the status of your working directory using “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)

So to remove files from index, you can apply the “git reset” command instead of the “git add” command which does exactly the opposite.

To Unstage All Files on GIT

In the first step, we have talked about how to unstage a file by its specific path or how to reset the file but sometimes you might have to unstage all the files at once from your index. To unstage all files, you can still use the “git reset” command without mentioning any specific path or filename:

$ git reset

The output will be this:

$ 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

Use the “git reset” command to unstage all the files and directories from the staging section and put them back in the 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/

Not a thing included to commit but pathless files present (use “git add” to track)

Follow the next method to remove the unstaged changes on Git.

Removing Unstaging Changes on Git

Sometimes you might have to remove the unstage files from your staging area manually using the “git checkout” command and along with that, don’t forget to mention the specific paths that you want to remove:

$ git checkout — <path>

If there is a file that is not staged in your directory, you will see this output:

$ git status

On branch master

Your branch is updated 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”)

To remove the changes you have done to this unstaged file, you can use the “git checkout” command and mention the file name:

$ git checkout — README

$ git status

On branch master

Your branch is up to date with ‘origin/master’.

nothing to commit, working tree clean

However, if you want to remove your overall directory, go back to the root of the design and execute this command:

$ git checkout — .

$ git status

On branch master

Your branch is up to date with ‘origin/master’.

nothing to commit, working tree clean

In case you want to unstage commits only, then use the “git reset” command and add a “soft” option along and the commit hash as well:

$ git reset –soft <commit>

To unstage your last commit, Head it to revert to the previous version of the commit:

$ git reset –soft HEAD~1

The “soft” argument is stored in your working directory and index and your changes are always kept but they are not in the Git repo. When you check out your repo after executing a soft reset, you will get this output:

$ 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

But if you hard reset your commit, all changes will be removed and you would use the “hard” command with “git reset” instead of “soft”.

$ git reset –hard <commit>

But when you are using the hard command, you will lose all your modifications and that’s why you should only apply it when necessary.

If you are using Git for years or for the first time, you should know that Git GUI also allows users to unstage files. Check out the next section to learn about Git GUI a little bit.

How to Unstage Files on Git GUI?

Many developers prefer to do commits using Git GUI but it is the easiest way to track all your changes. With the help of tcl-tk, you can see two lists in the dashboard of your Git GUI where you can find unstaged changes and the staged changes as well. You will find the changes you have added to the commit using ‘git add’ and you can also find the changes you have removed from the commit using ‘git rm’. Basically, the removed changes are found at the bottom of the list, and on the right side, you will find the current change of selected files and there you will also find a text area where you can add your own commit message. Git GUI is a fast command that allows you to view all the changes simultaneously and if you learn about shortcuts of GUI then the process will be easier.

  • You select files you want to stage or unstage and then press Ctrl+T/Ctrl+U to stage or unstage those files
  • Ctrl+I will stage all the files and will also ask you if you want to add new files
  • Ctrl+J is for reverting changes
  • Ctrl+Enter will help you perform a commit
  • Ctrl+P is for pushing the commit to the directory

So, if you don’t find unstaging and staging commits from the command line helpful or simple, then using graphical interfaces will help you to do so. But many people don’t like to use Git GUI because it does not look attractive and is a graphical application. So, it is up to you which process you are going to try to unstage or stage files on Git.

Conclusion

Let’s recap the whole process of Git unstage. Maybe you have created some junks in your working directory or added a new tool but accidentally staged some unwanted files to the Git repository. In that case, you can add those unwanted junks to your .gitignore file and unstage the changes that you have created. And this entire post only talks about how to unstage files of the Git repository through a few simple commands. Ensure that Git only adds the components that are not in the .gitignore files so that the unnecessary work doesn’t add up to your next commit. And since we have already talked about that in this post, you won’t have to worry about that too much.

Leave a Comment