I am trying to understand why Git has so many places to store changes. Imagine a simpler Git model that has only a local repository (and not a separate staging area or workspace). In this model:
In other words, we could do most of what Git now does, but without the conceptual clutter of a workspace, staging area, and local repository. Why is it useful to have all three?
These files are also referred to as "untracked files." Staging area is files that are going to be a part of the next commit, which lets git know what changes in the file are going to occur for the next commit. The repository contains all of a project's commits.
The repository is essentially the . git hidden folder inside the working directory (workspace). The working directory (workspace) is essentially your project folder.
Git local repository is the one on which we will make local changes, typically this local repository is on our computer. Git remote repository is the one of the server, typically a machine situated at 42 miles away.
There are three core areas to git. These are the Working Tree, the Staging Area (also known as Index), and the Local Repository. When working in a git repository files and modifications will travel from the Working Tree to the Staging Area and finish at the Local Repository.
There have been tomes written on git. Maybe what's missing for you are concrete examples of when a workspace and staging area would be useful.
1) You're working on this nifty idea for a few minutes, but it's not quite ready for your local repository. After a few min of tinkering, you realise it just isn't gunna work. You can simply git reset
your workspace to the local repository.
If you'd worked directly on the local repo, you'd have i) have dirtied your commit history, and ii) have to deal with a rollback.
2) Alternatively, say you want to merge two potentially conflicting branches. You can git merge --no-commit
, then clean-up manually within the workspace before staging and committing to the local repo.
1) You've hammered-out a bug-fix in your workspace. Looking at the backlog however, you realise the bug-fix is really in two conceptual parts, addressing two distinct underlying issues.
Having a staging area allows you to 'stage' first half the bug fix, and commit that to your local. Then, you can 'stage' the second half of the bug fix, and commit that separately.
2) Alternatively, take the example where you've made changes to a single file. Using the staging area you can commit just one portion of that file (via a patch) to the local repository. All changes would be in the 'workspace', but only a subset would be 'staged' for commit. I most often find myself staging partial files for READMEs and CHANGELOGs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With