Bird
Raised Fist0
Gitdevops~20 mins

What a branch is (pointer to a commit) 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 Branch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does a Git branch actually point to?

In Git, a branch is often described as a pointer. What exactly does this pointer reference?

AA branch points to a specific commit in the repository's history.
BA branch points to a remote repository URL.
CA branch points to a file in the working directory.
DA branch points to the latest tag in the repository.
Attempts:
2 left
💡 Hint

Think about what Git uses to track changes and history.

💻 Command Output
intermediate
2:00remaining
Output of 'git branch' command

What is the output of the git branch command immediately after creating a new branch named feature and switching to it?

Commands run:

git branch feature
git checkout feature
git branch
A
* main
  feature
B
  feature
* main
C
  main
  feature
D
* feature
  main
Attempts:
2 left
💡 Hint

The asterisk (*) marks the current branch.

🔀 Workflow
advanced
2:00remaining
Understanding branch pointer movement after commit

You create a new branch dev from main. You then make a commit on dev. What happens to the dev branch pointer?

AThe <code>main</code> branch pointer moves forward to the new commit.
BThe <code>dev</code> branch pointer moves forward to the new commit.
CBoth <code>dev</code> and <code>main</code> pointers move to the new commit.
DNeither branch pointer moves; only the working directory changes.
Attempts:
2 left
💡 Hint

Consider which branch you are on when making the commit.

Troubleshoot
advanced
2:00remaining
Why does 'git checkout' fail with 'pathspec' error?

You try to switch to a branch named feature using git checkout feature, but get this error:

error: pathspec 'feature' did not match any file(s) known to git

What is the most likely cause?

AThe branch <code>feature</code> does not exist locally or remotely.
BYou have uncommitted changes blocking the checkout.
CYou are not inside a Git repository.
DThe repository is corrupted and needs repair.
Attempts:
2 left
💡 Hint

Check if the branch exists before switching.

Best Practice
expert
2:00remaining
Why keep branches short-lived and merged frequently?

In Git workflows, why is it recommended to keep branches short-lived and merge them back often?

ATo avoid using tags for releases.
BTo increase the number of branches in the repository.
CTo reduce merge conflicts and keep history clean.
DTo prevent commits from being pushed to remote repositories.
Attempts:
2 left
💡 Hint

Think about teamwork and code integration.

Practice

(1/5)
1. What is a branch in Git?
easy
A. A backup of the entire repository
B. A pointer to a specific commit in the project history
C. A copy of all files in the project
D. A remote server where code is stored

Solution

  1. Step 1: Understand what a branch represents

    A branch in Git is not a copy of files but a reference to a commit.
  2. Step 2: Identify the correct description

    The branch points to a specific commit, allowing you to work on different versions safely.
  3. Final Answer:

    A pointer to a specific commit in the project history -> Option B
  4. Quick Check:

    Branch = pointer to commit [OK]
Hint: Remember: branch points, not copies files [OK]
Common Mistakes:
  • Thinking a branch copies all project files
  • Confusing branch with remote repository
  • Assuming branch is a backup
2. Which of the following commands correctly creates a new branch named feature in Git?
easy
A. git branch feature
B. git create branch feature
C. git new branch feature
D. git checkout feature

Solution

  1. Step 1: Recall the Git syntax for creating branches

    The correct command to create a branch is git branch <branch-name>.
  2. Step 2: Check each option

    Only git branch feature matches the correct syntax.
  3. Final Answer:

    git branch feature -> Option A
  4. Quick Check:

    Create branch = git branch [OK]
Hint: Use 'git branch' to create branches [OK]
Common Mistakes:
  • Using 'git create branch' which is invalid
  • Using 'git new branch' which is not a Git command
  • Confusing 'git checkout' with branch creation
3. Given the following Git commands:
git commit -m "Initial commit"
git branch feature
What does the feature branch point to immediately after creation?
medium
A. No commit, branch is empty
B. An empty commit with no changes
C. The first commit in the repository
D. The latest commit on the current branch

Solution

  1. Step 1: Understand branch creation behavior

    When you create a branch, it points to the current commit you are on.
  2. Step 2: Analyze the commands

    After the initial commit, creating 'feature' branch points it to that latest commit.
  3. Final Answer:

    The latest commit on the current branch -> Option D
  4. Quick Check:

    New branch points to current commit [OK]
Hint: New branch points where you are now [OK]
Common Mistakes:
  • Thinking branch points to no commit
  • Assuming branch points to first commit always
  • Confusing branch with empty commit
4. You ran git branch new-feature but the branch is not the current branch (no asterisk) when you run git branch. What is the most likely reason?
medium
A. You created the branch but did not switch to it
B. The branch creation command was incorrect
C. The branch was created on a remote repository only
D. Git does not allow branch names with hyphens

Solution

  1. Step 1: Understand branch creation and listing

    Creating a branch with git branch adds it locally but does not switch to it.
  2. Step 2: Check why branch might not be current

    The branch appears in git branch list but without the * marker because you did not switch (git checkout) to it.
  3. Final Answer:

    You created the branch but did not switch to it -> Option A
  4. Quick Check:

    Branch created but not checked out [OK]
Hint: Create branch then checkout to use it [OK]
Common Mistakes:
  • Assuming branch creation switches branches
  • Thinking branch names cannot have hyphens
  • Confusing local and remote branches
5. You want to create a new branch experiment that starts from a commit with hash abc1234, without switching to it. Which command correctly does this?
hard
A. git checkout -b experiment abc1234
B. git branch -c experiment abc1234
C. git branch experiment abc1234
D. git create branch experiment abc1234

Solution

  1. Step 1: Understand how to create branch at specific commit

    Using git branch <name> <commit> creates a branch pointing to that commit without switching.
  2. Step 2: Check each option

    git branch experiment abc1234 matches. git checkout -b creates and switches. C and D are invalid.
  3. Final Answer:

    git branch experiment abc1234 -> Option C
  4. Quick Check:

    Create branch at commit = git branch name commit [OK]
Hint: Use 'git branch name commit' to start branch at commit [OK]
Common Mistakes:
  • Using invalid commands like 'git create branch'
  • Confusing branch creation with checkout
  • Using wrong flags like '-c' which does not exist