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 Committing Changes

James Izzard
James Izzard
10,797 Points

What are the Implications of Using the -a Flag When Committing?

Hi All, In the video I believe the -a flag is described as requesting git to commit all changes that can be found between the folder and the repo. I tried the following to get a feel for how this works:

  1. I added two files to my project folder
  2. Tried commit with -a flag: git commit -a -m "useful message" Git responds by saying: 'nothing added to commit but untracked files present'.

So then I tried:

  1. Adding the files to version control: 'git add file-name' for each file
  2. Then I tried commit without the -a flag This causes both files to be committed.

So the following seems true:

  1. The -a flag doesn't commit ALL changed files to git unless they are already added to the repo.
  2. The -a flag isn't required to commit ALL changed files which have been added to the repo.

Would anybody be able to help me understand the exact function of the -a flag? Many Thanks James

2 Answers

Here's a link to the commit section of Git Reference: http://gitref.org/basic/#commit

And here's an excerpt:

git commit -a automatically stage all tracked, modified files before the commit

If you think the git add stage of the workflow is too cumbersome, Git allows you to skip that part with the -a option. This basically tells Git to run git add on any file that is "tracked" - that is, any file that was in your last commit and has been modified. This allows you to do a more Subversion style workflow if you want, simply editing files and then running git commit -a when you want to snapshot everything that has been changed. You still need to run git add to start tracking new files, though, just like Subversion.

James Izzard
James Izzard
10,797 Points

I understand. You still have to add files in the first place, but the -a flag will automatically stage all changed (but already added) files, and commit them. I was not making a distinction between adding new files to the repo and committing updates to files which had already been added. Thankyou

I understand that a new file has to be added so that git can track it. But I don't know what the concept of stage is. Why I need to 'git add' this file again to make it to the staging area before committing it?