git-add-preparando-cambios

Staging Changes with Git Add

  • 4 min

The git add command is the instruction responsible for moving changes from the Working Directory to the Staging Area.

As we’ve seen, the Staging Area is a characteristic “preparation zone” in Git that acts as an intermediate and mandatory waiting room through which everything must pass.

Once we save it “for real” by making a Commit (we’ll see this in the next article) that will be recorded in history forever.

So this Staging Area serves us to prepare what we want to save, calmly, before sending it to the repository.

To add files from our project to this preparation zone, we use git add.

The use of the Staging Area is “hidden” when using tools like VSCode, because they manage it transparently for us. We’ll see this when talking about GUI tools.

Adding specific files

The most basic way to use the command is by explicitly stating which file you want to stage.

git add nombre_del_archivo.ext
Copied!

Let’s consider a typical case:

  • You have three modified files, style.css, index.html, and server.js
  • But your change only affects the visual part.
  • The correct thing is to stage only the HTML and CSS.
git add index.html style.css
Copied!

If you do a git status now, you’ll see that server.js is still Modified, while the other two are Staged. You have just staged only these two files.

Adding everything

If you’ve finished a task and know that all the changes you’ve made in your folder are correct and should go together, you can use the wildcard:

git add .
Copied!

The dot . represents the current directory. This command adds all new, modified, and deleted files from the folder you are in downwards.

You will use this command many times because it’s very convenient. But it can also be dangerous. It’s very easy for a local configuration file, an error log, or a temporary image you didn’t want to upload to slip in.

Before doing git add ., get in the habit of always doing a git status to review what you’re about to put in the bag.

¿git add . vs git add -A?

In the past, there were notable differences. Nowadays (since Git 2.x), git add . captures almost everything.

  • git add .: Adds everything in the current folder and subfolders.
  • git add -A (or --all): Adds everything in the entire project, no matter which folder you are in.