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

What happens if i omit '-a' when committing a tracked, modified, but not-yet-staged file?

Hey friends,

I was doing a bit of experimentation with git, and I attempted to commit a file that was being tracked, but was not yet added to staging, without using the -a option, just to see what would happen. I used the following command:

git commit file1 -m "added 'qwerty'"

I expected to get an error and prompt to either use git add or -a in order to process the commit, but it just seemed to commit like normal. The log also showed that the change was committed.

What happened here? Any insight would be greatly appreciated, and I've included a larger snippet of my code below.

The-Book:project Chris$ nano file1
The-Book:project Chris$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file1

no changes added to commit (use "git add" and/or "git commit -a")
The-Book:project Chris$ git commit file1 -m "added 'qwerty'"
[master 95f5ef8] added 'qwerty'
 1 file changed, 1 insertion(+)

To anyone interested, the more i test this, the more it seems like including the filename in the commit command may "auto-stage" the specific file before committing, much like -a does for all modified files awaiting staging & commitment. Thoughts?

1 Answer

I see the following in the git man pages:

 -a, --all
       Tell the command to automatically stage files that have been modified
       and deleted, but new files you have not told Git about are not
       affected.

This tells me that if you omit the -a flag you will no longer auto-stage any of your modified/deleted tracked files. It will just attempt to commit whatever is currently in your staging area. If there is nothing staged (i.e. your staging area is empty) the commit command will issue an error message.

I suspect that this is the reason why you get the error message: there is nothing in your staging area because you did not provide the -a flag to your commit command.

You can separate the staging and committing in two separate command like this:

# add all modifications in the staging area
$ git add .

# do the actual commit
$ git commit -m "Some commit message."

Hope this helps.

Andrei,

Thanks for the advice, but if you re-read my post above, you'll notice that I did not receive an error message when i omitted -a and had nothing in staging. The files committed just as they would have had I included -a. This is what i found to be strange, but as you'll see in my note above, I have a theory that it may have to do with including the file name in the statement.

Hmm, interesting twist there... thanks for pointing that out. I guess I didn't read your post carefully enough.

I must confess I never tried to provide a list of files to the commit command. The way I usually control the "scope" of my commits is by controlling the staging area.

If, for example, I have a set of files modified & staged and I want to split that into several individual commits, I first un-stage all the files and re-stage them selectively for each individual commit.

It may be that the way you are doing it is actually a shortcut to the way I am used to do things. I'll have to check this out and do some testing on my own...

Thanks for your answer Andrei!

Either I'm confused or something isn't quite right with your answer.

I've tested this out with a new repository.

  1. Created new repository git init

  2. created README.md touch README.md

  3. Ok here is where I try to do the staging and commiting in one line git commit -a -m "added readme"

And I get the following message.

nothing added to commit but untracked files present.

Shouldn't that command stage the file automatically?

Cheers,

Seb