DEV Community

Cover image for Understanding Git EP01: The Five Zones of Git

Posted on • Updated on

Understanding Git EP01: The Five Zones of Git

Hello everyone.

Last week at work, a sudden requirement tested my git skills. I had to go through and understand two complex git use cases: git-subtree and git-filter-branch. While reading through long articles and manpages, I realised that even though I have been using this tool for years, I don't really understand what's happening under the hood.

So I set out to understand the tool, posting my notes from watching an intro course on Git Core Concepts that I found online.

The three main zones:

If you want to read the git documentation, it's very important to know the git terminologies so that you can understand what's written.

  1. Working directory is your code present in your filesystem. These are the regular files and folders and they have nothing to do with git.
  2. Index, also popularly referred to as "staging area", is where we gather our changes before making a commit out of it.
  3. The local git repository that forms the collection of all commits, and other version control data.

How to navigate between the first three zones:

Working Directory to Index: git add <file>...

Index back to Working Directory: git restore --staged <file>...

Index to Local repo: git commit -m "<commit message>"

Local repo to index: git reset -—soft HEAD~1

Local repo to the working directory: git reset -—mixed HEAD~1

Local repo to thin air (you lose the change): git reset —-keep HEAD~1

Commits have a link to their parent like a linked list. Merge commits have two parents. You can also get more than two in what is called "octopus".

Lifecycle of a file:

  1. Untracked: When a file is created, it's "untracked". Git doesn't know about it.
  2. Indexed: git add make it "indexed" / staged.
  3. Unchanged: git commit makes it "unchanged". The file on fs matches with what git knows.
  4. Modified: now when you modify this file that's tracked by git, it becomes "modified". Now when you stage and commit it, it goes through states (2) and (3) again.
  5. Removed: finally one last state is "removed" when you do a git rm and commit that change. It's just another commit that removes the file.

What is stash?

Building on the terminology from the last section, imagine there are some untracked and some indexed files, and you are not ready to commit yet. Suddenly your manager comes and tells you that there is a priority task that you need to immediately take up.

Stash is a fourth zone where you can keep aside (stash away) these tracked and untracked files temporarily while you jump to something else.

Move changes to the stash: git stash -u <file>...

Move changes back from the stash: git stash apply --index

Fifth zone: remote repo

You can push your commits to a remote shared git repo, and pull other people's commit from it into your local repo.

More git posts coming soon!

I do 1-1 mentoring for young developers, you can book a slot here if you are interested.

Top comments (0)