DEV Community

foadlind
foadlind

Posted on • Originally published at practicalgit.com

Staging vs. Committing: an explanation for Git beginners

I get these questions quite often:

"What is the difference between staging and committing?"

"Why is there a git add and a git commit? What's the difference?"

What is the difference?

Imagine you are doing online shopping. You browse through the pages of your favorite online store and add some products to your shopping cart. The shopping cart is like staging. Things are there ready to be paid for. You can keep adding stuff to it or remove some from it.

When you are done shopping, you go and pay for the things in your shopping cart. That can be compared to making a commit. Now there is a record somewhere of what you have bought and when. There is a history.

Notice that you can easily change your mind when things are in staging, but once you have made a commit things are hard to change (although not impossible!).

Why are they separated?

The fact that you need to stage and commit separately in Git has many benefits. Generally it is a good idea to make your commits small. And to make sure that all the changes that are included in one commit are related to each other.

For example a commit that fixes a bug in Feature A should not also include an improvement you made to Feature B. If you have been productive 😎 and have done those two things at once, you should not commit everything at once. You should separate them by staging the bug fixes and committing those first and then staging the improvements to Feature B and making a second commit.

That way, if someone looks at the Git history of your project, they are not confused by one giant commit that did a lot of things. They can see clearly that you performed two different tasks: one bug fix in Feature A and one improvement to Feature B.

How to stage and commit

Ok so now that you know the difference between staging and committing, let's see how to do these things in Git:

To add the changes you made to a file to staging, use this command

$ git add <path-to-file>
Enter fullscreen mode Exit fullscreen mode

If you want to add all the files at once, put a dot instead of path to each file, like this:

$ git add . 
Enter fullscreen mode Exit fullscreen mode

All the changes will be staged, ready to be committed.

To commit use the command:

$ git commit -m "some descriptive message"
Enter fullscreen mode Exit fullscreen mode

I have created a Git cheat sheet for beginners to make it easier to look up these basic commands. You can grab a free copy from here.

Top comments (0)