nickackerman42 nickackerman42 - 1 year ago 87
Git Question

Why does Git have a Workspace, Staging Area, and Local Repository?

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:

  1. We could pick files in the local repository we want Git to track

  2. We could track changes by taking snapshots of the repository at different points in time

  3. We could fetch changes to origin/master and merge as usual

  4. We could have multiple branches and switch between them

  5. We could push changes from the local repository to "origin" as usual

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?

Answer Source

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.

Staging area

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download