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

Tomasz Sporys
Tomasz Sporys
11,258 Points

Bit confused about when add something to staging area and when to commit in Git

Hi I'm not sure if I'm thinking right about the way you update repository within Git. Tell me if I'm, right, please.

step 1. You creating new file (example style.css)

step2. You are adding it to staging area using "git add command". Here not sure: every time you have done some progress with the file or any other file in the project you are using git add command to update files?

step3. When Do I sue git commit? If I update files using git add, how do I know if its ready to commit?

It's confusing! Let me see if I can break it down a little.

When you say:

$ git add filename.txt

you are saying that you want to stage that file for your next commit.

Now lets assume you have three files with associated changes in them. You can stage those files with associated changes so that you can group them into the same commit.

$ git add filename.html
$ git add filename.css
$ git add filename.js
$ git commit -m "Fixed left-pad issue"

When you stage files you are saying, "these files all have changes that I want committed." And when you commit you are saying, "these changes are all a part of the same implementation for a feature or bug." And we do that so we can have a history of our changes.

So, in answer to your third question, you ought to commit when you have a completed task. You add and commit changes that are a part of that task. Staging is so that if you have changes to other files that you want in a separate commit, you can add files for one commit, do that commit, then add the other files and do the next commit. Staging is a way to keep changes separate from each other in commits.

Extra Credit

In the future, you may want to get more specific with what you add and commit. Don't worry about it too much now. But for future reference and posterities sake. Here is an example:

$ git add -p

will take you through all changes to files in your local repo. Then you can say "yay" or "nay" to staging each specific change within each modified file. It will allow you to be more granular with your commits.

And I highly recommend staying far away from the lazy route of:

$ git add .

Hope that helps!

Tomasz Sporys
Tomasz Sporys
11,258 Points

Yes it helped. Thanks for the effort. :)