Add Files to the Staging Area

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a popular version control system that allows developers to track changes made to their codebase. One of the essential features of Git is the staging area, which allows developers to selectively choose which changes to commit. In this lab, you will learn how to add files to the staging area using Git.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/add("`Stage Files`") subgraph Lab Skills git/add -.-> lab-12761{{"`Add Files to the Staging Area`"}} end

Add Files to the Staging Area

You have been working on a project stored in a Git repository named https://github.com/labex-labs/git-playground. You have made some changes to the codebase and want to commit these changes to the repository. However, you only want to commit specific changes and not all the changes you have made. To do this, you need to add the files to the staging area.

  1. You will make some changes in the git-playground directory:
echo "hello" > index.html
echo "world" > style.css
echo "git" > one.js
echo "labex" > two.js
echo "hello git" > 1.py
echo "hello labex" > 2.py
  1. Add these files to the staging area:
git add index.html style.css
  1. View the status of the current working directory and staging area, including information on which files have been modified, which files have been added to the staging area, etc:
git status
  1. Alternatively, add all files with a .js extension:
git add *.js
  1. View the status of the current working directory and staging area again:
git status
  1. You can also add all changes to the staging area:
git add .
  1. View the status of the current working directory and staging area again:
git status

This is the finished result:

Summary

In this lab, you learned how to add files to the staging area using Git. You can use the git add command to add specific files or a fileglob to add multiple files to the staging area. Adding files to the staging area allows you to selectively choose which changes to commit to the repository.

Other Git Tutorials you may like