Git: the basic commands

The everyday local commands, status, add, commit, diff, restore and log: what each one reads or changes in the working directory, the index and the repository.

  • 22 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 status: what is it?

    Show the answer

    Shows the current branch and the state of the files: staged, modified but not staged, untracked.

    It changes nothing. It is the reflex before any commit: it tells what will go, what will not, and the command to change that.

  • You have just run git add a.txt with no other change. What does git diff show?

    • Nothing: the working directory and the index are identical
    • The changes that git add has just staged
    • The changes to a.txt since the last commit
    • An error: a file or a commit must be given
    Show the answer

    Nothing: the working directory and the index are identical

    git diff compares the working directory with the index. Once the file is added, they are the same. To see what was just staged, use git diff --staged.

  • git … turns the current folder into a repository by creating the .git folder.

    Show the answer

    git init turns the current folder into a repository by creating the .git folder.

    No project file is touched. The files already there stay untracked until the first git add.

  • git add: what is it?

    Show the answer

    Copies the current state of a file into the index; if the file was new, it becomes tracked.

    The state copied is that of the moment: a change made afterwards needs another add to go into the commit.

  • config.txt was staged by mistake, but you want to keep its changes in the file. What do you do?

    • git restore config.txt
    • git rm config.txt
    • git reset --hard
    • git restore --staged config.txt
    Show the answer

    git restore --staged config.txt

    Only the first one takes the file out of the index while keeping its content. git restore config.txt copies the index back into the file: the file stays staged. git rm refuses here (“changes staged in the index”) and would aim to delete the file anyway. git reset --hard overwrites all uncommitted work.