Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 573
nickackerman42 Avatar asked Oct 31 '16 17:10

nickackerman42


People also ask

Why is there a staging area in git?

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.

What is difference between repository and workspace?

The repository is essentially the . git hidden folder inside the working directory (workspace). The working directory (workspace) is essentially your project folder.

What is the use of local repository in git?

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.

What are the 3 local areas associated with a git repository?

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.


1 Answers

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.

Working tree / Work tree

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.

like image 141
rmharrison Avatar answered Oct 17 '22 09:10

rmharrison