Back to Blog
Lesson 6 of the Git & GitHub: Git & GitHub from Zero course
GitJuly 17, 20263 min read

Tracking Files: How to Use git add for Version Control

Learn how to create files and use git add to move them into the staging area. Master the first step in creating a permanent snapshot of your project work.

gitversion controlcommand linestaginggit add
Close-up of software development tools displaying code and version control systems on a computer monitor.

Previously in this course, we covered the conceptual Understanding the Git Lifecycle: Staging Area & Working Directory. Now that you understand the "why" behind the staging area, this lesson focuses on the "how": using the git add command to move your work from the working directory into the staging area to begin tracking your files.

The Role of Staging in Your Workflow

In Git, "tracking" a file means telling the version control system that it should watch this file for future changes. Before you create a permanent record (a commit), you must place your files into the staging area.

Think of the staging area as a "pre-commit" workspace. It allows you to selectively choose which file modifications are ready to be saved, giving you granular control over what goes into your project history.

Practical Example: Tracking a New File

Let’s advance our running project. Assume we have already initialized our repository as taught in Initializing a Repository: Start Your Git Project Today.

  1. Create a file: Open your terminal in your project directory and create a new file named notes.txt.

    Bash
    touch notes.txt
  2. Check the status: Use git status to see how Git views this new file.

    Bash
    git status

    You will see notes.txt listed under "Untracked files." This means Git sees the file on your disk, but it isn't part of your project's version history yet.

  3. Stage the file: Use the git add command to move the file into the staging area.

    Bash
    git add notes.txt
  4. Verify the change: Run git status again.

    Bash
    git status

    Now, notes.txt appears under "Changes to be committed." It is officially being tracked.

Hands-on Exercise

To build your working competence, try this sequence in your own terminal:

  1. Create a file named README.md using echo "My Project" > README.md.
  2. Run git status and confirm it is "Untracked."
  3. Run git add README.md to stage it.
  4. Run git status one last time to confirm it has moved to the "Changes to be committed" section.

Common Pitfalls

  • Forgetting to add new files: A common mistake is assuming that because a file exists in the directory, Git is automatically tracking it. You must explicitly git add every new file.
  • Adding the wrong files: If you accidentally add a file you didn't mean to (like a temporary log or system file), don't panic. We will cover how to "unstage" files in a later lesson.
  • Over-adding: Beginners often use git add . to add every single file in a folder. While convenient, it is better to be explicit (e.g., git add filename) as you learn, so you don't accidentally include sensitive files or build artifacts in your commit.

Frequently Asked Questions

Does git add save the file to the repository? No. It only moves the file to the staging area. The file is not saved to the project history until you perform a git commit.

Can I use git add on files I have already added? Yes. If you modify a file that is already being tracked, you must run git add again to stage those new changes before committing them.

What happens if I edit a file after I've already staged it? The version you staged remains in the staging area, but your new edits in the working directory remain unstaged. You would need to run git add again to include the latest edits in the next commit.

Recap

Tracking files is the bridge between your local edits and your permanent project history. By using git add, you move files from the untracked state into the staging area, effectively telling Git: "I am ready for these changes to be part of the next snapshot."

Up next: We will take these staged files and finalize them in our history by Creating Your First Commit.

Similar Posts