You've already dipped your toes into the git add command in the Git Status lesson, and now it's time to dive deeper. In this lesson, you'll build on what you've learned of the standard Git workflow so far by utilizing git add in the following scenarios:
- Git Add: File
- Git Add: Multiple Files
- Git Add: Using a Wildcard Pattern
- Git Add: All
What is Git Add?
The git add command adds the changes you've made in your working directory to the Git staging area. It tells Git that you intend to include the changes made to a file in the next commit.
You'll learn more about what a commit is soon, but for now, think of it as the next and final step in storing the changes from your staging area. Therefore, git add does not affect the version-controlled Git repository until you run git commit.
Git Add Example
You may remember running the git add command in the git status lesson. This exercise will expand on what you learned, but will start with a clean slate. So, in this git add example, you'll initialize a new repo and create a file to practice running the command.
To set it up, complete the following steps to create a new directory containing some example files.
1. Create a New Directory
As with previous lessons, you can create a new directory within your Documents/ directory.
Note: Since you'll be initializing a fresh Git repo, be sure you're not doing so in a pre-existing one.
In your terminal, run the following Bash commands to create a directory and navigate to it:
$ cd ~/Documents
$ mkdir add_example
$ cd add_example/
2. Create a Sample File
Using the touch command, create a sample file:
$ touch Practice.txt
3. Initialize a Git Repository
Run git init to create a new and empty Git repo in the current working directory. You can then run git status to check your work:
$ git init
Your output should look something like this:
Initialized empty Git repository in /Users/admin/Documents/add_example/.git/
Now check the status of your Git repository:
$ git status
Git notifies you that you have an untracked file called Practice.txt, and that you can use git add to include it in what will be committed. That means your repo is initialized and ready to track changes. Good work!
Git Add: File
Now that you've initialized your new repo, run git add to add the file:
$ git add Practice.txt
This adds the Practice.txt file to the staging area.
To see the updated status of your new git repository, run git status again. You'll see that Git knows about the changes and is ready for you to commit them.
Running git add saves a snapshot of the new file and adds it to the staging area. This command is the first step in the process of putting your edits under version control. You will use it often when you're working with Git.
Note: The file is not yet version-controlled. Only after running git commit will the changes be added to the Git repository. You'll learn more about the git commit command shortly.
Now, what about a situation where you have 200 new files that you need to add under version control? Going through the above process 200 times doesn't sound like a great investment of your time. Fortunately, Git provides some useful shortcuts and process improvements when you run the git add command.
Git Add: Multiple Files
Assume your working directory contains three more files called my-file1.txt, my_file2.txt, and another-file.html. These are clearly important files, so you want to add all of these to the staging area to move them a step closer to being version-controlled. Rather than adding them separately, though, you could follow up the git add command with each of the file paths, separated by spaces.
$ git add my-file1.txt my_file2.txt another-file.html
Great! While this still requires some typing, it definitely speeds the process up. But what if you wanted to add all .txt files while leaving out the .html file?
Git Add: Wildcard
You can also add multiple files using a Git wildcard pattern, which adds all files with a similar pattern in their names. For example, say you want to add all the .txt files from above:
$ git add *.txt
Alternatively, you could add only the HTML files with the following command:
$ git add *.html
Neat! But what about those 200 files you conjured further up...?
Git Add: All
When you run git add, all your new files and changes can be added in one fell swoop:
$ git add .
This will add everything that has changed since the last commit to Git's staging area. The . stands for the current folder, so it literally means, "add everything in the current folder".
Note: Don't use the . shortcut unless you are very sure of what you're doing. When using it, it's very easy to add files to the staging area mistakenly. When you start collaborating on a project with other developers, adding files to the repository that shouldn't be there is a mistake that makes everyone's life harder, and you should avoid it as much as possible.
Even if you are just getting started and working on your own repository, you might accidentally push your passwords to a public GitHub repo without being aware of it, which immediately compromises it and makes the service whose password was leaked insecure. Always be aware of what you are adding.
While these blanket shortcuts can sometimes be useful, keep in mind that when you add files individually, you will have absolute control over what gets added to your staging area and what stays out.
Info: As Git says in the git status output, you can "unstage" a git add operation by running git rm --cached <file>. You will learn more about this command in later sections of this course.
Summary: Git Add
In this lesson, you've learned how to use the git add command to add the changes you make in your working directory to the Git staging area. You've practiced using this powerful command to:
Git Add: File
$ git add <file>
Git Add: Multiple Files
$ git add <file> <file> <file>
Git Add: Wildcard
$ git add *<pattern>
Git Add: All
$ git add .
Now that you've practiced using git add, next up is a video lesson about using the git add command.