Concept of git tracking and git staging

When you modify a file in your working directory, git tells you to use “git add” to stage.

When you add a new file to your working directory, git tells you to use “git add” to start tracking.

  • Meaning of Git checkout double dashes
  • git switch branch without discarding local changes
  • List submodules in a git repository
  • git: how to “retrack” untracked file
  • Receiving “fatal: Not a git repository” when attempting to remote add a Git repo
  • Is there an exclude file-equivalent for pushing in git?
  • I am a bit confused about these 2 concepts because i assumed tracking a file for changes is different from staging it for commit

  • github generate ssh key on windows
  • Encrypted version control
  • For “git diff” is there a -U<infinity> option to show the whole file?
  • Code Lens not working properly after interactive rebase
  • Git pre-commit hook to find text in files
  • There isn't anything to compare. Nothing to compare, branches are entirely different commit histories
  • 4 Solutions collect form web for “Concept of git tracking and git staging”

    Git has a concept known as ‘the index’. To create a new commit, you fill the index with the contents you’d like to have in the next commit. That means that you have to explicitly tell Git which changes you want to appear in the next commit by using git add. (git add -p to add only single hunks)

    It doesn’t make a difference to Git whether you only update a file (»stage changes«) or if you add the full content of a new file (»start tracking a file«) – both times, all that Git’s index sees is the addition of new changes

    Git essentially has 4 main statuses for the files in your local repo:

    • untracked: The file is new, Git knows nothing about it. If you git add <file>, it becomes:
    • staged: Now Git knows the file (tracked), but also made it part of the next commit batch (called the index). If you git commit, it becomes:
    • unchanged: The file has not changed since its last commit. If you modify it, it becomes:
    • unstaged: Modified but not part of the next commit yet. You can stage it again with git add

    As you can see, a git add will track untracked files, and stage any file.

    Also: You can untrack an uncommited file with git rm --cached filename and unstage a staged file with git reset HEAD <file>

    When you add a file to start tracking, it also stages its contents.

    If you want to add a file for tracking without staging it, you can use

    git add -N

    Both of the git add steps you identify do essentially the same thing, they simply have different explanations because of their arrival route.

    The git add simply tells git that the file provided is a file that you desire to have, in its exact current form (its content), within its source control repository. At that point git will take a snapshot of the file (and it keeps a note in its index) so that it is ready for when you have all your changes to your files ready and added (i.e marshalled together in the staging area), for your git commit (with appropriate message ;-).

    Once git has been told about that file (e.g. @avh’s -N option) it will notice (track) changes to the file under the guise of various commands (such as git status). Thus, later, you have to explicitly tell git when you no longer want a file to be tracked (git rm <file>), and you can continue editing a file (locally) after you have added the version that will be in the commit. Almost obviously (or perhaps not), you can git add a file many times before you commit the final version.

    Git Baby is a git and github fan, let's start git clone.