Git Add - Saving changes in staging area

Git

Share

In this tutorial we will learn to save changes in Git repository.

git add

We use the git add command when we want to add the changes in the working directory to the staging area.



If we want to stage (add) all changes in a file we pass the file path.

$ git add path/to/file

Example

$ git add index.php

If we want to stage (add) all changes in a directory we pass the directory path.

$ git add path/to/directory

Example

$ git add src/

If we want to stage (add) all changes in the working directory we use .

$ git add .

Going back to our git-project that we created in the previous tutorial, if we run the git status command we will get the status of the Git repository.

$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

From the above output we can easily tell that we are on the master branch and there is nothing to commit.

Now we will create a new file index.php inside the project folder using touch index.php command.

$ touch index.php

Now, if we run the git status command we will get an untracked file.

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

	index.php

nothing added to commit but untracked files present (use "git add" to track)

So, the output tells us that there are some untracked files. In this case, index.php file.

Untracked files: A file that is not yet staged or committed.

To add a change in a file to the staging area we use git add [file] command.

$ git add index.php

Now if we run the git status command we will see that the index.php file is staged and ready to be committed.

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

	new file:   index.php

Now, lets create a js directory inside the project folder and add a default.js file inside it.

$ mkdir js
$ touch js/default.js

Now if we run the git status command we will see untracked files and staged file index.php the one we added earlier.

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

	new file:   index.php

Untracked files:
  (use "git add ..." to include in what will be committed)

	js/

In the above output we can see that we have js/ directory that is untracked.

To stage the js directory we will run the following command.

$ git add js/

Now, the git status command will show us all the files that are staged.

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

	new file:   index.php
	new file:   js/default.js

git reset

To unstage a file that was staged using the git add command we use the git reset [file] command.

If we want to unstage lets say default.js file which is inside the js folder we will use the following command.

$ git reset js/default.js

Now, if we run the git status command it will show js/ as untracked.

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

	new file:   index.php

Untracked files:
  (use "git add ..." to include in what will be committed)

	js/

To unstage all staged files use git reset command without filename.

In the next tutorial we will learn to commit the changes that were added to the staging area.