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 Merging Simple Merges

Thelma Boamah
Thelma Boamah
15,726 Points

I don't understand when you use the -a flag while committing to Git? Can someone please explain?

When you commit to Git, how do you determine when to use the -a flag?

3 Answers

"git add ." will add any changes to your stagging area. Anything staged gets committed. "git commit -a" will add AND commit what follows. so you can "git commit -a index.html" and it will both add and commit the changes to index.html - but not changes to other files. The flag acts as a shortcut so you don't have to "git add index.html" and "git commit index.html"

Thelma Boamah
Thelma Boamah
15,726 Points

Oooh got it. Thank you, Karen.

jack AM
jack AM
2,618 Points

Sure no worries. Let's say you have one file that you told git about by doing the following.

git add file.txt

And then moments later you make a change to that file in your text editor...Now if you were to do

git commit

by itself, without the -a, in order for your changes to actual be committed to git, you would first have to re-add the file, and THEN commit it, like so...

git add file.txt
git commit

another change 10 minutes later,

git add file.txt
git commit

and then another change 10 minutes later,

git add file.txt
git commit

And so you would have to do this re-adding process every time you want to commit changes to file.txt...Now an easier route is, since git starts tracking your files after the initial add command, instead of re-adding the file every time you want to commit changes, you just throw the -a flag after the commit command. Like so...

git commit -a file.txt

another change 10 minutes later,

git commit -a file.txt

And so it shortens the process of having to type things out.

Thelma Boamah
Thelma Boamah
15,726 Points

OK, got it now. It wasn't clicking that -a let's you skip the add step. I appreciate the detailed reply. Thanks!

jack AM
jack AM
2,618 Points

The -a flag for commiting just tells git that you want all your files commited. It would be the same as giving it the --all flag, however, it will not commit any files that you havent told git about. Meaning that if you had just created/moved a file into the git directory that you're working on, but didn't do a git add command for that file, when you commit your changes, git will not include that file.

Thelma Boamah
Thelma Boamah
15,726 Points

Thanks for the response, Jack. Does that differ from what would happen if you did git commit without the -a flag? Still don't quite get it yet.