The git add command adds a file or folder to the staging area. Files in the staging area are those that you want to add to your next commit. Git add does not modify or otherwise affect your repository or files.
The Git version control system does not have a simple save feature. Git uses a process called committing. This is where you create a record of all the changes made to files since the last commit.
This process involves a few different command line operations. The first step in saving file or folder changes to a Git repository is to use the git add command. This command lets you select which files you want to commit to a repository by moving the directory to the staging area.
This tutorial will discuss, with examples, how to use the git add command to select the files you want to commit to a repository. By the end of reading this tutorial, you’ll be an expert at using the git add command.
Saving Files in Git
When you save a file on your computer, the computer stores the changes on your local machine. For instance, when you save a Word document, your computer will store the new version of the document.
Git, on the other hand, uses a different approach to saving changes. When you save a file in a project you are working on, the changes will be saved on your computer. However, in order for your Git repository to reflect the changes, you need to commit those changes to the repository. The following flowchart reflects how to do this:
After you change a file you retrieved from a Git repository, the repository itself will not change unless you go through these steps. While your computer will store the new version of the file, the main repository—which is usually stored on another server—will not change. Furthermore, Git will not keep track of the changes you made to the file until you commit the updated file(s) to the repository.
The workflow for changing a file or folder in a Git repository looks like this:
- On your computer, update a file or folder in the Git repository.
- Use git add to add those changes to the staging area.
- Use git commit to move changes from the staging area to a commit.
- Use git push to push those changes to the main repository.
To save a file in Git, you need to use the git add and git commit commands. In this tutorial, we discuss the first of these—the git add command.
What is the git add Command?
The git add command adds a file to the Git staging area. This area contains a list of all the files you have recently changed. Your repository will be updated the next time you create a commit with your changes.
Therefore, running the git add command does not change any of your work in the Git repository. Changes are only made to your repository when you execute the git commit command.
The syntax for the git add command is as follows:
git add fileName
The file you specify can be any file or folder in your Git repository.
Suppose we want to add a file called
README.md to the staging area because we want to commit it to our main repository. We can do so using this code:
git add README.md
This command adds the file README.md to the staging area.
There is a way to apply the above command to all updated files and folders from our repository. The code for this is:
git add --all
This command adds every change we made to files and folders from our repository to the Git staging area.
Now that we’ve added files to the staging area, we can proceed to commit them to our repository using the git commit command. We discuss the git commit command in our git commit guide.
Git Add All Files
You can add all the files in a repository to the staging area using the git add -A command or the git add . command.
The -A flag indicates that you want to add all files to a repository. This flag is shorthand for –all. You can use –all instead if you prefer. Let’s add all the changes we’ve made to the staging area:
git add -A
Our staging area now contains all the changes we have made to our files.
We can also use the git add . command. This command adds all the files in the folder you are presently viewing to the staging area.
To add all files to the staging area using the git add . command, you must be viewing the root folder in your project. This is the main folder in which all of your code is stored.
Let’s use the git add . command:
git add .
We’ve added all the files in our repository to the staging area.
Git Staging Area
Throughout this tutorial, we refer to the idea of a staging area in Git. This staging area is a unique feature of the Git version control system. It acts as a buffer between the unsaved codebase changes and the history of the project.
The staging area is a useful feature. It allows you to make changes to files in a repository without actually changing the repository itself. You can make edits to local files and later commit them to the repository.
One potential use case for this is if you need to make changes to a number of files. If so, you may want to split up your changes into multiple commits. This will help you ensure you don’t push too many changes to your repository at once. This is generally bad practice, because it makes it more difficult to keep track of the changes made between commits.
By using git add, you can selectively add certain files to a commit.
Version control is all about creating commits that are simple to track. Having easily trackable commits helps developers when they need to identify the source of any coding problems that arise.
The git add command is the first command in a series of operations used to save changes to a Git repository. The command sends changes you made to a staging area. You can then use git commit to commit those changes to the main repository.
This tutorial discussed, with examples, how to use the git add command to add files and folders to the Git staging area. Now you’re ready to start using the git add command like a professional programmer!