Ben Forshey

Git Add

written January 29th, 2018

I’ve found myself referencing the git documentation (or Stack Overflow) for git add a few times, so here’s a quick note on the commands I use most often to add files to be tracked by Git.

I know that the Git CLI changes with version upgrades—so this tutuorial is written with the assumption that you’re using a version greater than or equal to 2.0.

git add --all

Longhand for git add -A. I find myself using this command most often. It feels easiest to type.

git add .

There’s a lot of old information floating around the internet for this one. Prior to Git version 2.0, git add . didn’t process removals. So, if you had deleted a file, git add . wouldn’t untrack it from your working tree (what gets tracked in version control). That’s no longer true past version 2.0.

The . is interpreted by Git as a pathspec. In bash (and maybe other shells?), the . is synonymous with the current working directory. So you’re telling Git to add everything not ignored (defined by your .gitignore) in the current working directory, recursively (in all subdirectories). This does not traverse the parent directories (or the entire working tree, in Git-speak).

git add -A

Git’s documentation:

Update the index not only where the working tree has a file matching <pathspec> but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.

If no <pathspec> is given when -A option is used, all files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).

So what’s the difference between git add . and git add -A? Importantly, without a pathspec, git add -A will add every file in the entire working tree, whereas git add . will not if you are not in the root directory. That’s because the . passes in the current working directory to the pathspec. If you’re in a subdirectory, your pathspec doesn’t include parent folders.

Summary

So, if you’re always in the habit of running git add from nested folders within your Git root directory, perhaps use git add -A. By habit, I only like to work git commands from the same level as my .git folder (the root directory), so I think I’m good to just use git add .. Either way, it’s handy to know the difference.