3

I am trying to better understand how git works.

So we have a working directory (our project files), a staging area (a collection of changes ready to go), and a git repository (where those changes are applied to when committed).

However I am struggling to understand because it seems like files being "tracked" or not refer to some specific thing, and yet we still say each section does its own form of tracking?

Is it correct to say:

  1. If I add or modify a file in my working directory, git detects all of this. Do we say that these files become "tracked" by git at this point? Or are they merely "detected" (or something else)? Is everything in the project folder / working directory "tracked"?

  2. git add {directory or filename} appears to add the contents to the staging area, yes? At this point do we say all staged files are "tracked" files, the ones that show up when we do git ls-files, which I believe shows the files in the staging area? Or no?

  3. git commit -a moves all the changes in the staging area to the repository.

Is this correct?

The 29th Saltshaker
  • 671
  • 1
  • 6
  • 16

2 Answers2

2
  1. If I add or modify a file in my working directory, git detects all of this. Do we say that these files become "tracked" by git at this point? Or are they merely "detected" (or something else)? Is everything in the project folder / working directory "tracked"?

Git detects modified and deleted files. If they are tracked (i.e. they were added before and committed) they appear in the output of git status as "modified". The new files and the modified files that are not tracked appear as "untracked".

  1. git add {directory or filename} appears to add the contents to the staging area, yes? At this point do we say all staged files are "tracked" files, the ones that show up when we do git ls-files, which I believe shows the files in the staging area? Or no?

The staging area is used to prepare the next commit. You can say the files it contains are "tracked" but its content is not permanent; it can be changed any time. In order to turn a new file really "tracked" by Git you have to commit the staged changes.

  1. git commit -a moves all the changes in the staging area to the repository.

git commit creates a new commit using the staged content. git commit -a automatically stage tracked files that were changed or deleted and then operates the commit. The new files are not added.

Read more about git commit and about how Git records the changes to the repository.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Added before to where? Modified in what context? Are all stages files tracked? Are all tracked files staged? But commit -a stages tracked files that were changed or deleted, but this was also used to define tracked? – The 29th Saltshaker Jul 24 '17 at 21:12
  • "Added before" means "git add". Modified means modified. No context is required. Just fire and editor and change an existing file. Read the documentation page I linked in the last sentence of my answer. The answer to your questions about what exactly "tracked" means is [there](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository), in the second paragraph. Read the entire [Git book](https://git-scm.com/book/en/v2) to learn how Git works. – axiac Jul 24 '17 at 22:15
  • Next you'll probably ask "What is a snapshot?". It is explained in the book in the ["1.3 Getting Started - Git Basics"](https://git-scm.com/book/en/v2/Getting-Started-Git-Basics) chapter. – axiac Jul 24 '17 at 22:18
1

git add adds files to be tracked as well as staging (the current commit)

git commit -a adds files to staging only if they are already tracked

Git notices files that are not on tracked when they are added to the repo directory because it understands them as a file change. But unless you explicitly add them to the tracking it will not track any change increments

This question may help you: Concept of git tracking and git staging

Also see this https://www.howtogit.net/concepts/types-of-changes.html

I think the main point of our confusion is that git add can do two things (add to tracking if not already, and stage) whereas git commit -a only does one (add tracked files to the stage)

Toby
  • 9,696
  • 16
  • 68
  • 132
  • So the "index" is another state? Files that have been "add"ed at some point? Is there some kind of explanation for the difference between indexed, tracked, staged, committed, etc? And I thought committing moved data *from* the staging area to the repo? – The 29th Saltshaker Jul 24 '17 at 21:02
  • 1
    @The29thSaltshaker the terms `index` and `stage` are used interchangeably. They denote the same thing: a Git feature that the user uses to prepare the content of the next commit. – axiac Jul 24 '17 at 21:06
  • But he mentioned that git add adds files to the index and the staging, implying they are different things – The 29th Saltshaker Jul 24 '17 at 21:09
  • @axiac Hmm, then I guess I didn't use quite the right words... Where I have "index" I actually meant "tracked" – Toby Jul 24 '17 at 21:09
  • 1
    @The29thSaltshaker: for tracked or not, and what the index does here, see my answer to https://stackoverflow.com/q/45291442/1256452 – torek Jul 25 '17 at 06:20