Bird
Raised Fist0
Gitdevops~20 mins

Repository (committed history) in Git - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Git History Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of git log --oneline -3?

You run the command git log --oneline -3 in a repository with 5 commits. What will this command show?

Git
git log --oneline -3
AShows the first 3 commits made in the repository with short hashes.
BShows all 5 commits with full commit hashes and detailed commit messages.
CShows the last 3 commits with their short commit hashes and commit messages.
DShows only the commit messages of the last 3 commits without hashes.
Attempts:
2 left
💡 Hint

Think about what --oneline and -3 do in git log.

🧠 Conceptual
intermediate
1:30remaining
What does the HEAD pointer represent in a Git repository?

In Git, what is the role of the HEAD pointer?

A<code>HEAD</code> points to the latest commit on the current branch you are working on.
B<code>HEAD</code> is the name of the remote repository.
C<code>HEAD</code> stores the list of all branches in the repository.
D<code>HEAD</code> is a backup of the repository before the last commit.
Attempts:
2 left
💡 Hint

Think about what Git uses to know your current working snapshot.

Troubleshoot
advanced
2:30remaining
Why does git log --graph show a disconnected commit?

You run git log --graph and see a commit that is not connected to the main branch history. What could cause this?

Git
git log --graph
AThe commit was deleted but still appears due to caching.
BThe commit is corrupted and Git cannot link it properly.
CThe repository is empty and has no commits.
DThe commit belongs to a branch that was never merged into the main branch.
Attempts:
2 left
💡 Hint

Think about how branches and merges affect commit history graphs.

🔀 Workflow
advanced
2:30remaining
What is the effect of git reset --hard HEAD~2?

What happens when you run git reset --hard HEAD~2 in your repository?

Git
git reset --hard HEAD~2
AMoves the current branch pointer back by 2 commits and resets the working directory and staging area to match that commit.
BDeletes the last 2 commits permanently from the remote repository.
CCreates a new branch named <code>HEAD~2</code>.
DStashes the last 2 commits for later use.
Attempts:
2 left
💡 Hint

Consider what reset --hard does to the branch pointer and files.

Best Practice
expert
3:00remaining
Which Git command safely shows the commit history including merges with a clear visual graph?

You want to see the full commit history including merges, with a clear visual graph and branch names. Which command is best?

Agit diff --graph
Bgit log --graph --oneline --all --decorate
Cgit show --graph
Dgit log --stat
Attempts:
2 left
💡 Hint

Look for options that show graph, all branches, and decorate with branch names.

Practice

(1/5)
1. What does the git log command show in a Git repository?
easy
A. A list of all commits made in the repository history
B. The current status of files in the working directory
C. The list of branches in the repository
D. The remote repository URLs configured

Solution

  1. Step 1: Understand the purpose of git log

    The git log command is designed to show the commit history of the repository, listing all saved changes.
  2. Step 2: Differentiate from other commands

    Commands like git status show file changes, git branch shows branches, and git remote -v shows remote URLs, not commit history.
  3. Final Answer:

    A list of all commits made in the repository history -> Option A
  4. Quick Check:

    Commit history = git log output [OK]
Hint: Remember: git log = commit history list [OK]
Common Mistakes:
  • Confusing git log with git status
  • Thinking git log shows branches
  • Mixing git log with remote info commands
2. Which of the following commands correctly shows a short, one-line summary of each commit in the history?
easy
A. git log --short
B. git log --oneline
C. git show --summary
D. git status --oneline

Solution

  1. Step 1: Identify the correct flag for short commit view

    The --oneline option with git log shows each commit in a single line summary.
  2. Step 2: Verify other options are incorrect

    git log --short is invalid, git show --summary shows details of one commit, and git status --oneline is invalid syntax.
  3. Final Answer:

    git log --oneline -> Option B
  4. Quick Check:

    Short commit list = git log --oneline [OK]
Hint: Use --oneline with git log for brief commit list [OK]
Common Mistakes:
  • Using invalid flags like --short
  • Confusing git show with git log
  • Trying to use git status for commit history
3. Given this command output from git log --oneline:
f3a1b2c Fix typo in README
9d8e7f6 Add new feature X
4b3c2d1 Initial commit
What is the hash of the commit that added the new feature?
medium
A. 9d8e7f6
B. f3a1b2c
C. 4b3c2d1
D. Cannot tell from this output

Solution

  1. Step 1: Read the commit messages and hashes

    The commit message "Add new feature X" corresponds to the hash 9d8e7f6.
  2. Step 2: Match the message to the correct hash

    Each line shows the hash first, then the message. So the hash for adding the feature is 9d8e7f6.
  3. Final Answer:

    9d8e7f6 -> Option A
  4. Quick Check:

    Commit message matches hash 9d8e7f6 [OK]
Hint: First word is hash, rest is message in git log --oneline [OK]
Common Mistakes:
  • Picking the wrong hash for the message
  • Thinking hashes are at the end
  • Assuming output is incomplete
4. You ran git log --oneline but got an error: error: unknown option `--oneline'. What is the most likely cause?
medium
A. You typed the command in the wrong folder
B. You must run git fetch before using --oneline
C. You need to add git log --oneline --all to fix it
D. Your Git version is too old and does not support --oneline

Solution

  1. Step 1: Understand the error message

    The error says the option --oneline is unknown, meaning Git does not recognize it.
  2. Step 2: Identify the cause

    This usually happens if the Git version is old and does not support the --oneline flag.
  3. Final Answer:

    Your Git version is too old and does not support --oneline -> Option D
  4. Quick Check:

    Unknown option error = outdated Git version [OK]
Hint: Unknown option often means Git version is outdated [OK]
Common Mistakes:
  • Assuming wrong folder causes option error
  • Thinking fetch fixes option errors
  • Adding unrelated flags to fix syntax
5. You want to see a quick summary of the last 3 commits in your repository, each on one line. Which command should you use?
hard
A. git log --oneline --last 3
B. git log --3 --oneline
C. git log --oneline -n 3
D. git log --limit=3 --oneline

Solution

  1. Step 1: Identify the correct option to limit commits

    The -n 3 or --max-count=3 option limits the number of commits shown.
  2. Step 2: Combine with --oneline for short output

    Using git log --oneline -n 3 shows the last 3 commits in one-line format.
  3. Step 3: Check other options for correctness

    --3 is invalid, --last 3 and --limit=3 are not valid git log options.
  4. Final Answer:

    git log --oneline -n 3 -> Option C
  5. Quick Check:

    Limit commits with -n, short view with --oneline [OK]
Hint: Use -n with --oneline to limit commits shown [OK]
Common Mistakes:
  • Using invalid flags like --last or --limit
  • Trying --3 which is invalid
  • Mixing order of options incorrectly