The Git add command places new or changed files into the staging area so you can prepare them for the next commit. It does not save the work yet, but marks the files that you want Git to store later.
Table of Content
Understand the git add Command
Git add puts your file or folder into a staged state. Git will then know which files you want to save on the next commit. It does not store history itself but only marks changes.
Here is the command:
git add <file-or-path>Replace <file-or-path> with the name of a file or a folder. Git then moves that file or folder to the staged area.
You can add one file with this form:
git add index.htmlThis command puts only the index.html file in the staged area so the next commit will include it.
You can add more than one file by typing each name:
git add file1.js file2.cssThis command moves both file1.js and file2.css to the staged area so the next commit will include both files.
You can add every change in the current folder:
git add .This command stages every new or changed file in the current folder so the next commit will include all of them.
The Difference Between git add and git commit
Git add only marks files for the next commit. Git commit creates a snapshot in the Git history. You can use git add many times before you run git commit. Git commit will then store all staged files at once. Git add prepares data but git commit records data. You use git add to collect files and git commit to save them.
Examples
Add a new HTML file:
git add about.htmlThis command marks the about.html file to be included in the next commit, so the change enters Git history after you run git commit.
Add every new and changed file:
git add .This command places every changed or new file in the current folder into the staged area so the next commit records all these files.
Add files from a subfolder:
git add src/This command stages all files inside the src folder so the next commit records that whole folder in one action.
Add only files with a name pattern:
git add *.jsThis command stages every file that ends with .js so the next commit records all these JavaScript files.
Wrapping Up
You learned what git add does and how to use it with files, folders, and patterns.
Here is a quick recap:
- git add marks files for commit but does not save them, git commit stores staged files in history, and you can use git add in many ways to control what goes into the next commit.
FAQs
What is Git add command used for?
git add command stages changes in your project.
It tells Git which files you want to include in the next commit.
Example:
git add file.txt How do I add all files with Git add?
git add .
Alternative option:
git add -A
- git add . stages changes in current folder
- git add -A stages all changes in repo
What is the difference between Git add and Git commit?
git addtells Git which files to track.git commitsaves those changes in history.
git add index.html
git commit -m "Add index file"Can I undo a Git add command?
git reset file.txt
To remove all staged files:
git reset Similar Reads
You can use the Git config command to set user data and control Git behavior. It saves preferences for projects…
If you've worked on any coding project, you would know this frustrating wall where Git simply refuses to pull the…
One of the tools we need for software development is a Version Control System. It tracks code changes, making a…
When you work with teams, you often need to remove old remote branches. To delete a remote branch in Git,…
Git status command shows the state of files in your project. It shows files in the staged area, files not…
When you work on a project with friends, one writes the intro, one adds pictures, and others handle different parts.…
The purpose of the Git Push command is to upload local repository changes to a remote repository. Pushing your committed…
Git helps you manage code in different branches. You can delete a branch when it’s old or not being used.…
The switch operation in Git will make it easier. Designed to simplify the process for developers, it's safer and more…
The moment you dive into programming or development, the first thing you are going to bump into is Git. It's…