4

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?

2
  • 1
    Possible duplicate of Git: add vs push vs commit Commented Jul 24, 2017 at 20:59
  • @Jonas I am asking a slightly different question than this, but that is still a helpful link, thanks Commented Jul 24, 2017 at 21:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
"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, in the second paragraph. Read the entire Git book to learn how Git works.
Next you'll probably ask "What is a snapshot?". It is explained in the book in the "1.3 Getting Started - Git Basics" chapter.
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)

5 Comments

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?
@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.
But he mentioned that git add adds files to the index and the staging, implying they are different things
@axiac Hmm, then I guess I didn't use quite the right words... Where I have "index" I actually meant "tracked"
@The29thSaltshaker: for tracked or not, and what the index does here, see my answer to stackoverflow.com/q/45291442/1256452

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.