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.

Advertisement

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.