Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Development Tools Git Basics Getting Started With Git Committing Changes

Mohammed Safiulla D
Mohammed Safiulla D
12,621 Points

Use of -a flag in git commit command

Guys I just want to know the difference between git commit and git commit -a. What I think is git commit only commits newly created files i.e, committing for the first time and git commit -a commits updates to already existing files i.e, these file have been already committed once and the new files. Is my understanding correct? Please explain in simple words as I am very new to git.

2 Answers

Ryan Ruscett
Ryan Ruscett
23,307 Points

Hey,

Here is how the flow works. Say I have 10 files. I made changes to 5 of them and created 5 more. I have two choices.

  1. I can do "Git add file1", "Git add file2" etc etc

This will take my changes and add them to a "staging" area. From there I can do a git commit, which will commit the changes to my local repository. If I wanted to push those changes to a remote repository, I would do "git push"

  1. I could use a shortcut. "Git commit -a" Will automatically add any files that were changed to the "Staging" area as well as commit them. It's a shortcut.

If you add files or change files in a repository. You can do a "git status" . This will show you untracked files (newly added files) and any files that have had changes made to them. Normally you would would do git add to add all the files, but what if I made 20 files and don't want to write git add for all 20 files before doing a git commit. Remember, I can only commit files and changes to files that I have added first. If I wanted to add all of these at once. I would do a git commit -a which will take all files in a repository new or changed and add/commit in one swoop. Be careful with this though. Sometimes your forget you added a file and might commit something you didn't want.

Does this make sense?

Shawn O'Connor
Shawn O'Connor
14,135 Points

Files have to be added to the staging area in order to commit. -a is the equivalent of git add prior to a commit.

Mohammed Safiulla D
Mohammed Safiulla D
12,621 Points

I didn't know about the staging area, changes not staged and the untracked files now after Ryan's explanation and working a simple example in my system it all makes sense....thanks!