Git Command Line Essentials Quiz Quiz

Challenge your understanding of key Git command line operations with practical questions designed to enhance your workflow efficiency and version control skills. This quiz covers crucial commands, option usage, staging, branching, and commit management within cli-tools, tailored to boost your command line expertise.

  1. Staging Files for Commit

    Which Git command stages all modified and new files in the current directory and its subdirectories for the next commit?

    1. git add .
    2. git push all
    3. git commit -a
    4. git init .

    Explanation: The command 'git add .' stages every changed and new file in the current directory and below, preparing them for commit. 'git push all' is not a valid command and would result in an error. 'git commit -a' commits changes to tracked files but does not include new untracked files. 'git init .' initializes a new repository, not stage files for commit.

  2. Viewing Commit History

    If you want to see a concise list of previous commits with their messages and short hash values, which Git command should you use?

    1. git log --oneline
    2. git history -short
    3. git commit --list
    4. git status -l

    Explanation: 'git log --oneline' provides a shortened, one-line-per-commit view of the commit history, making it easy to scan. 'git history -short' and 'git commit --list' are not valid Git commands. 'git status -l' does not exist in Git; 'git status' is used to show the state of the working directory, not the commit history.

  3. Undoing Local Changes

    Suppose you modified 'config.txt' but haven’t staged it yet; which command will discard your local changes and restore the file to its last committed state?

    1. git checkout -- config.txt
    2. git remove config.txt
    3. git ignore config.txt
    4. git revert config.txt

    Explanation: 'git checkout -- config.txt' reverts the file to its last committed version, discarding uncommitted changes. 'git remove config.txt' deletes the file from the working directory and stages the deletion, which is not a safe way to discard edits. 'git ignore config.txt' has no meaning in Git. 'git revert config.txt' is not a valid command; 'git revert' works on commits, not individual files.

  4. Branch Creation and Switching

    To simultaneously create and switch to a new branch named 'feature-xyz', which Git command should you use?

    1. git checkout -b feature-xyz
    2. git create-branch feature-xyz
    3. git branch -sw feature-xyz
    4. git switch --new feature-xyz

    Explanation: 'git checkout -b feature-xyz' creates a new branch and switches to it in one step. 'git create-branch feature-xyz' is not a valid Git command. 'git branch -sw feature-xyz' is incorrect as it combines unrelated and invalid options. 'git switch --new feature-xyz' is not a recognized Git command; while 'git switch' with '-c' can create a branch, the exact option here is not correct.

  5. Amending the Last Commit

    After committing, you realize you forgot to include a staged file in your last commit; which command allows you to add it to the previous commit without creating a new one?

    1. git commit --amend
    2. git save --append
    3. git add-amend
    4. git modify commit

    Explanation: 'git commit --amend' allows you to modify the most recent commit, integrating additional staged changes or updating the commit message. 'git save --append' and 'git add-amend' are not Git commands, and 'git modify commit' does not exist in Git. Only 'git commit --amend' properly updates the last commit as described.