Skip to main content

My Mental Model for Git

· 4 min read
Erik Montes

Git can be difficult to understand, but I have developed a mental model that helps me better understand it, and hopefully it will help you too!

Let's Use Analogies

First of all, think of yourself as a professional photographer with a Polaroid camera, an album to store the photos you take, and also you having a stage to place things you want to take pictures of. Lastly, you have a time machine!

Now, instead of thinking of files and folder, think of people and houses, respectively. You're the photographer so you can decide what people can wear and how to decorate a home at any time. (Editing files and folders).

When you change someones outfit, you might like it. So after complimenting them you'll want to take a picture of them, however, there's one restriction, you can only take picture of people if they're on the stage. So you need to kindly tell them to do that (git add). Though sometimes you might decide last minute you don't want to take a picture of them, so you can kindly tell all or some of them to get off (git restore --staged).

Assuming you're happy with everyone that is on the stage now you can snap a picture of them (git commit and write a little message on the back of the picture (git commit -m "a little message here"). This picture will be then be stored in your photo album. You can always see the contents of your album (git log). And now that you have an album with some pictures you can travel back in time to whatever picture you want (git checkout <id of picture>!

Now you might have a lot of important pictures in your album and you don't want to lose them. What you can do is make a copy and upload the copy to somewhere else (git push), you decide where and how often to upload copies.

Flow

This is the typical pattern of the steps, note that you can go either direction on each step.

Edit files (working area) ↔ add on stage (staging area) ↔ commit ↔ push to remote (optional)

Analogy Summary

  • files → people 👨‍👩‍👧‍👦
  • folders → houses 🏠
  • editing files/folders → changing peoples outfit or home decor 🔄
  • git restore <files or folders> → you didn't like the outfit so you want to put back their previous outfit ↩️
  • git add <files or folders> → you're happy with the outfits so you tell all or some of the people you want to take a picture of to get on a stage in preparation to take a picture of them 👉
  • git restore <files or folders> --staged → you changed your mind, so you have people get off the stage 😏
  • git commit → taking a photo 📸
  • git commit -m "a nice little note" → writing on the photo ✍️
  • git diff → comparing two photos side by side, with new things shown in green and things that were removed shown in red 🧐
  • git status → Provides the answer to "what's going on? 🤔" You can see who's on the stage or who's outfit you've changed, or maybe neither is the case
  • git log → viewing your full photo album, all the memories 🥹
  • git checkout <reference> → inserting reference data to travel to it. For example you can get a photo from your photo album and place it in your time machine and time travel to it! 🔮
  • git push → upload a copy of your album to a remote location i.e. Github, Gitlab, Bitbucket, etc. ☁️