Git: repository, index, commit

Git's model: working directory, index and repository, what a commit contains, the hash that identifies it, HEAD and the four states of a file.

  • 24 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Git repository: what is it?

    Show the answer

    The database, kept in the .git folder at the root of the project, that holds the whole history of commits.

    Deleting the .git folder deletes the history but leaves the project files: the working directory is only a working copy of one version.

  • In this git status, a.txt appears in two sections. What will a.txt contain in the next commit?

    ```text
    On branch main
    Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
    modified: a.txt

    Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
    modified: a.txt

    Untracked files:
    (use "git add <file>..." to include in what will be committed)
    b.txt
    c.txt
    `

    • The version staged by the last git add
    • The current version of the file, with all its changes
    • None: Git refuses a file listed in both sections
    • The version from the last commit, without any change
    Show the answer

    The version staged by the last git add

    The index keeps the file as it was at the time of git add. A change made afterwards is in the “Changes not staged” section: another git add is needed for it to go.

  • The files you edit are in the …, those of the next commit in the …, and the history in the ….

    Show the answer

    The files you edit are in the working directory, those of the next commit in the index, and the history in the repository.

    A commit records a copy of the index in the repository, and the index does not empty for all that; what you changed without adding it stays only in the working directory.

  • Index (staging area): what is it?

    Show the answer

    The intermediate area where you compose the exact content of the next commit, file by file.

    It lets you commit only part of the changes under way: whatever was not added to the index does not go into the commit.

  • a.txt and b.txt are both tracked and both modified. You want to commit only b.txt. Which commands do it?

    • git commit -a -m "Fix b"
    • git add . then git commit -m "Fix b"
    • git commit -m "Fix b" with nothing added first
    • git add b.txt then git commit -m "Fix b"
    Show the answer

    git add b.txt then git commit -m "Fix b"

    Only what is in the index goes into the commit. -a and add . put a.txt in as well; with no git add, the index is empty and Git answers “no changes added to commit”.