How to unstage the changes in Git – git rm command

HOME

The previous example has explained the staging of new files to the staging area.

What is git rm?

This command removes the file from the staging area. The files from the working directory will remain intact. This means that you’ll still have a copy of the file locally. The file will be removed from the index tracking of Git project.

Let us explain this with the help of an example.

  1. Create an empty Git RepositoryGitTest
  2. Create a new file – ReadMe.txt and add the file to staging area.
  3. Create another file – Index.html
  4. Update the first file – ReadMe.txt which is already staged.
  5. Use “git add .” to stage both the files.

Later, we realized that we don’t want to commit both the files. We try to commit the logically same files, like files created or changed to fix a particular bug. So, if in the future we want to track back the changes, it is easy to figure out which all files are changed or impacted.

We want to unstage the “Index.txt” file.

Use the below command to reset the changes from the staging area to the working directory.

git rm --cached Index.txt

The “cached” option specifies that the removal should happen only on the staging index.

I have used “git status” command and it shows that “ReadMe.txt” can either be unstage or commit. Whereas “Index.txt” is unstage. So, if we want to commit Index.txt, first we need to stage this file using git add command.

This image shows that git rm moves the staged file from Staging Area to back to working Directory.

I hope this tutorial has helped you to understand git rm command.

How to stage changes in Git – git add Command

HOME

The previous tutorial explains the creation of a Git Repository. This tutorial explains the steps to stage the changes in the Git Repository.

We have a new Git Repository named as GitTest. Go to the Git Repository and Right-click and select “Git Bash Here”.

To know how to create a new empty Git Repository, refer to this tutorial – How to create a new Git Repository – git init Command.

Type “git status” command on Git Bash and press enter.

This shows that there is no uncommitted file available in the Git Repository.

Scenario 1 – Adding/Staging a new file to Git

Now, let us create a new file called – ReadMe.txt in the Git Repository. Again, type the command – “git status”.

When we have added a new file and use the Git Status command, Git shows that the newly created file is an untracked file, since it is not yet tracked by GIT. It is showing a suggestion to use the “git add” command with the file name so that it can be included as a part of the next commit.

As suggested by Git, let us use the “git add” command to commit the file and then use “git status” command to see the status of Git.

When we ran the “git add” command, then changes from the working directory were copied (not moved) to the staging area. 

The primary function of the git add command is to promote pending changes (new file or altered file) in the working directory, to the git staging area. The staging area is one of Git’s more unique features.

Scenario 2 – Adding a new file to git and updating the already staged file

I have created another new file – “Index.txt” and updated the already staged file – “ReadMe.txt”. Type the command “git status” and see the message provided by Git.

This image provides 4 suggestions

  1. If we want to unstage the old file – ReadMe.txt, we can do that by using command “git rm”.
  2. The latest changes are not staged for ReadMe.txt file. This can be done by using “git add” command.
  3. If we want to move the latest changes from staging area to working directory, use “git restore” command.
  4. The new file Index.txt is not at all staged. So, it can be staged by using the command “git add“.

There are multiple ways to stage multiple files in Git.

The first way is to add the name of all the files, as shown below.

 git add ReadMe.txt Index.txt

The second way is to use the below command. This will stage all the unstaged files available in Git.

git add .

Staging Files – This diagram helps you to remember the difference between Staging Area and Git Repository.

I hope this has helped you to understand the difference between Staging Area and Git Repository.

How to create a new Git Repository – git init Command

HOME

I have explained the steps to install Git on Windows 10 in the last tutorial – How to install Git on Windows 10. This tutorial will explain the process to create a new Git Repository.

Git offers a better way of keeping track of changes by using the Git repository. A repository is a place that stores something like files, data, code, etc.

What is Git Repository?

A Git Repository stores file and keep track of the changes. A Git repository is a virtual storage of a project. It allows you to save versions of your code, which you can access when it is needed. That’s why all software project uses a version control system like Git. Git automatically will not start tracking changes to any file. We need to let git know where to track and what to track.

Let us understand how to use git init command.

Step 1 – Create an empty directory – GitTest.

Step 2 – Right-click and click on Git Bash Here.

Step 3 – Type a git command as “git status”.

As the new directory is not a git directory it showed a fatal messagefatal: not a git repository (or any of the parent directories): .git.

What is git status command?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. Status output does not show you any information regarding the committed project history.

Step 4 – Type git command – “git init”.

Here, you can see an empty git repository is created under C:/GitTest.

What is git init command?

git init is a one-time command which you use during the initial setup of a new repo. Executing this command will create a new .git subdirectory in your current working directory. This will also create a new main branch – master branch. Master or Main are default branch.

The git init” command creates a new hidden folder named.gitin the same directory where it was run.

Running git init in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates (or to move the repository to another place if –separate-git-dir is given).

Congratulation!!! We have understood the use of git init command.