Welcome to the Treehouse Community
Looking to learn something new?
Mohammed Safiulla D12,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.
Ryan Ruscett23,307 Points
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.
- 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"
- 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'Connor14,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.