The Git Stage is like your Shopping Basket

Git concepts like the Stage can be confusing at first, so I've come up with an analogy that I hope will help someone relate better.

The Shopping Basket

Today is Hug Your Cat Day, and you're planning to cook dinner for the whole family to celebrate. You'll be cooking your favourite meal - the family-secret Stew. Your cousin Tom is also a cat-lover and will contribute to the desert.

You go to your corner store, pick up a shopping basket and begin browsing. You pick up all the ingrediates for the stew - carrots, tomatoes, beef, herbs, lemon, chillis, coriander, potatoes, amongst other things. You go to the checkout and pay for the groceries before returning home and start preparing the stew.

Once the stew is cooking nicely, your cousin Tom goes to the store to pick up dessert. He first picked up a lemon meringue cheesecake, some soda pop and was just going to the counter when the most magnificant blueberry pie caught his eye; it was love at first sight and the cheesecake had to go.

Tom pays for the groceries and trotted home.

That night everyone in the family had the most enjoyable dinner, cats were hugged, and this story ends happily.

The Git Stage

In Git, you'd make changes to the files in your working directory. You'd then pick which changes you want to be included by adding them to the stage. This is like picking which produce to add to your shopping basket.

The stage is like a temporary area for you to group multiple, related changes together to commit in one go. The basket is a temporary container for you to house your groceries together so you can check them all out in one go.

And just like how Tom can remove the cheesecake from his shopping basket, you can add files to and from the stage too.

Once you're happy with your selection of changes on stage, you can then commit everything. At that time, you're storing the changes into the Git database, where it lives permanently. Just like once you're happy with the ingrediants, you pay for it; then the groceries are yours and you can't just go back and refund it (store policy).

In fact, you can skip the stage altogether and commit each file individually, but this is like paying for each ingrediant one at a time - you'll end up with many receipts and it's much harder to keep track of your expenses that way.

Normally, you'd stage relevant changes together, just like how you bought the ingrediants for the stew, and Tom bought the deserts - this keeps things organized.

If two people worked on different features (like you and Tom), they can each make, stage and commit their own changes. Furthermore, if at any time we need to revert back the changes, we only have to revert back the relevant ones.

Altogether Now

Here's the general workflow for grocery shopping:

  1. Decide which ingrediants you want
  2. Add/remove the ingrediants in your basket
  3. Pay for the groceries

And here's the general workflow for using Git:

  1. Make changes to the source code
  2. Stage the changes
  3. Commit the changes
comments powered by Disqus