git commit -am “Added new file”

The above command is something I used on a project recently as a shortcut to stage and commit the file within the same command. Cool! That saves me from running an extra command. Before, I would stage and track all the files that I’ve been working on with the following:

git add .

followed by the actual commit itself for the staged changes made:

git commit -m "Added new file"

I tried this new found shortcut recently in a similar scenario but was surprised to see that it failed. Huh? I’ve done this before, I changed a file and then was able to combine the command to both stage and commit it at once. The key here (if you believe my commit message) is that in this scenario we have added a new file. This is a file that hasn’t been tracked before so git isn’t aware of it. Git even points this out to us right after we try and execute the command for a file that has never been tracked before.

git_am_fail

We’ll have to specify that this file should be tracked and staged by using the command¬†git add Test.rtf and then commit the file with git commit -m "Added new file".

Now, if we modify the contents of the Test.rtf file for any future changes we can use the add and commit combined command.

git_am_success

This is because I didn’t have a clear understanding of what git commit -a would do. When adding the -a flag to git commit this instructs Git to automatically stage every file that is already tracked before doing the commit. While git add filename both tracks and stages the file specified.