Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Git Basics Getting Started With Git The Staging Area

Sebastian Eguez
Sebastian Eguez
8,248 Points

When do we git commit w/ "-a"?

When do we

git commit -a -m "message"

versus just

git commit -m "message"

?

2 Answers

(Edit) Yup Kevin is correct... we all learned something new.

The -a flag is adding the changes to the staging area. You are preparing the content for the next commit. Before every commit you must add the changes to the staging area.

git add .
#git status will show what is staged for the next commit
git status 
git commit -m 'commit message'

If you were to just want to commit specific changes you'd have to put those specific files in stage like so..

git add index.html main.css
git commit -m 'updated with foo'
Kevin Korte
Kevin Korte
28,148 Points

Correct me if I'm wrong, but

EDIT I'm checking into this, my virtual box is V 1.9.1. Git V 2.+ has changed a bit, so I edited this answer to reflect Git 2.+, not my old 1.9 version.

git add .
git commit -m 'commit message'

is not the same as

git commit -a -m 'commit message'

And why I think that's the case is git add . stages all files, new and untracked, untracked and modified, and removed.

However, git commit -a -m 'commit message' or git commit -am 'commit message' only stages and than commits previously tracked, and modified files, or tracked files that are now removed. If there are new files, they won't be picked up in this commit.

I think it's more correct to say

git add -u
git commit -m 'commit message'

is the same as

git commit -a -m 'commit message'

as git add -u only stages previously tracked and modified files, or removed files, but it doesn't stage untracked files.

If you wanted to get everything in one fell swoop, you could git add -A or git add . and this will stage new, untracked files that are not ignored, tracked files that have been modified, and tracked files that have been removed, and stage all for the commit.

So the big difference here is that git add . does stage new files, while `git commit -a -m 'commit message' does not stage new files.

**Again, this is in V2.0+. My current version is 1.9.1 and git add . operates differently in that it does not stage untracked files for commit. Something to be aware of, and that I learned today.

Thanks for this. The man pages for git aren't as helpful as they might be, and it would be good to get a concise and brief update to the syntax changes for the recent versions that are relevant to our level of git knowledge and interest. As of today I'm running version 2.11.0