git add puts local modified files and folders into a staging area. After a
git commit can be used to turn the changes into a commit, and
git push can be used to push those changes to the main repository.
The Git version control system does not have a simple
save feature you can use to save your code to files in a repository. Instead, it uses a process called
committing, which commits a set of files and directories to a repository and saves the changes made to those files to the repository’s history.
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.
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.
Git Saving Files
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.
The git add command adds a change made to a local file to the staging area in Git. (The
staging area contains a list of all the files you have recently changed. We discuss the staging area in greater depth in a later section of this tutorial.) Git will make these changes to the corresponding files in the repository 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 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 because 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 to 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. Furthermore, Git’s staging area makes it easier for developers to push smaller commits that contain only the code they are ready to share with the rest of the development team.
The git add command is the first command in a series of operations used to save to a Git repository the changes you made to files and folders therefrom. 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!