Branching & Merging Questions with Answers

22 practice questions on Branching & Merging (Git & DevOps) with answers and step-by-step solutions. Try 10 samples below, then practise all 22 free in the AptiNest app.

Sample questions

Question 1Easy

Which command lists the local branches?

  1. git remote
  2. git branch
  3. git list
  4. git show-branches --all

Answer: B. git branch

Explanation

git branch lists local branches and marks the current one with *. Add -a to include remote-tracking branches.

Step-by-step solution

  1. git branch with no arguments lists local branches.
  2. The current branch is marked with *.
  3. -a also shows remote-tracking branches.

Why the other options are wrong

  • A. git remote lists remotes, not branches.
  • C. git list is not a Git command.
  • D. show-branches is not a Git command (git show-branch exists but shows commits).

Tip: git branch = local; -r = remote; -a = all.

Question 2Easy

Which command creates a branch dev and switches to it (Git 2.23+)?

  1. git switch dev
  2. git switch -c dev
  3. git branch -m dev
  4. git merge dev

Answer: B. git switch -c dev

Explanation

switch -c creates and switches. git checkout -b dev does the same.

Step-by-step solution

  1. -c means create.
  2. Git adds the branch at HEAD.
  3. HEAD moves to it.

Why the other options are wrong

  • A. Without -c, the branch must already exist.
  • C. -m renames.
  • D. merge integrates branches.

Question 3Easy

What does git switch - do?

  1. Switches back to the previously checked-out branch
  2. Deletes the current branch and then switches to the default branch
  3. Switches to the first commit
  4. Lists all branches

Answer: A. Switches back to the previously checked-out branch

Explanation

The - shortcut means the previous branch, like cd - in a shell.

Step-by-step solution

  1. Git remembers the last branch.
  2. - refers to it.
  3. HEAD moves back.

Why the other options are wrong

  • B. Delete is branch -d.
  • C. Not the meaning of -.
  • D. Listing is git branch.

Question 4Easy

Which command creates a new branch named feature and switches to it?

  1. git checkout feature --new
  2. git switch -c feature
  3. git branch feature --go
  4. git merge feature

Answer: B. git switch -c feature

Explanation

git switch -c feature (or the older git checkout -b feature) creates the branch and switches to it in one step.

Step-by-step solution

  1. git switch changes branches.
  2. -c creates the branch first.
  3. So git switch -c feature creates and switches (older form: git checkout -b feature).

Why the other options are wrong

  • A. --new is not a checkout option.
  • C. --go is not a git branch option.
  • D. git merge combines branches.

Tip: Create and switch: switch -c or checkout -b.

Question 5Medium

In a file with a merge conflict, what does the section between <<<<<<< HEAD and ======= contain?

  1. The version from the branch being merged in
  2. The version from your current branch
  3. Git's suggested resolution
  4. The common ancestor's version

Answer: B. The version from your current branch

Explanation

The part after <<<<<<< HEAD is your current branch's version, and the part between ======= and >>>>>>> branch-name is the incoming version.

Step-by-step solution

  1. Conflict markers split the two versions.
  2. Between <<<<<<< HEAD and ======= is your current branch's version.
  3. Between ======= and >>>>>>> is the incoming branch's version.

Why the other options are wrong

  • A. The incoming version is after =======.
  • C. Git does not propose a resolution in the markers.
  • D. The common ancestor appears only with diff3 style, in a ||||||| section.

Tip: HEAD on top = yours; bottom = theirs.

Question 6Medium

What happens when you git checkout other while holding uncommitted edits to a file that differs between the two branches?

  1. Changes are silently discarded so that the checkout can always succeed
  2. Git refuses and asks you to commit or stash first
  3. Git auto-commits them
  4. Changes are silently carried and always merged

Answer: B. Git refuses and asks you to commit or stash first

Explanation

To protect uncommitted work, Git aborts the switch if it would overwrite local edits.

Step-by-step solution

  1. Edits touch a file that differs.
  2. Git detects the overwrite risk.
  3. It stops with an error.

Why the other options are wrong

  • A. Git never silently discards edits.
  • C. Git never commits on its own.
  • D. It carries edits only if no conflict arises.

Question 7Medium

What does git merge --no-ff feature do?

  1. Squashes all commits into one
  2. Always creates a merge commit, even when a fast-forward is possible
  3. Refuses to merge if a fast-forward is possible
  4. Merges without checking for conflicts

Answer: B. Always creates a merge commit, even when a fast-forward is possible

Explanation

--no-ff forces a merge commit, which keeps a visible record that a feature branch existed.

Step-by-step solution

  1. Normally Git fast-forwards when it can.
  2. --no-ff forces a merge commit anyway.
  3. This records that a feature branch existed and was merged.

Why the other options are wrong

  • A. Squashing is --squash.
  • C. --ff-only refuses non-fast-forward merges; --no-ff does the opposite.
  • D. Conflicts are always checked.

Tip: --no-ff = always a merge commit.

Question 8Medium

A merge commit created by merging two diverged branches has how many parents?

  1. One
  2. Two
  3. Three
  4. Zero

Answer: B. Two

Explanation

A true merge commit records the tip of both lines, so it has two parents. Fast-forward merges create no commit.

Step-by-step solution

  1. Both branch tips are joined.
  2. The commit has two parent pointers.
  3. HEAD^2 is the merged-in tip.

Why the other options are wrong

  • A. That is a normal commit.
  • C. That would be an octopus merge of three branches.
  • D. Only a root commit has zero.

Question 9Hard

Given this history, which command lists commits that are on feature but not on main?

A---B---C   main
     \
      D---E   feature
  1. git log feature..main
  2. git log main...feature
  3. git log feature --not feature
  4. git log main..feature

Answer: D. git log main..feature

Explanation

X..Y means reachable from Y but not from X, so main..feature gives E and D.

Step-by-step solution

  1. main..feature excludes everything reachable from main.
  2. That removes A and B.
  3. Leaves D and E.

Why the other options are wrong

  • A. That lists C only.
  • B. Three dots would list D, E and C (symmetric difference).
  • C. feature --not feature is empty.

Tip: Two dots = in right, not in left.

Question 10Hard

What does git cherry-pick <hash> do?

  1. Merges the entire branch that contains that commit into the current one
  2. Moves the commit to the current branch and deletes it from its original branch
  3. Checks out that commit in detached HEAD state so you can inspect it
  4. Applies that commit's changes as a new commit here, with a new hash

Answer: D. Applies that commit's changes as a new commit here, with a new hash

Explanation

cherry-pick copies a commit's changes. The new commit has a different parent, so it gets a different hash, and the original commit is left unchanged.

Step-by-step solution

  1. cherry-pick takes the changes introduced by one commit.
  2. It applies them on the current branch as a new commit.
  3. The new commit has a different parent, so a different hash; the original stays.

Why the other options are wrong

  • A. It copies one commit, not a whole branch.
  • B. The original commit is not removed.
  • C. That is git checkout <hash>.

Tip: Cherry-pick copies; it does not move.

Practise all 22 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Git & DevOps topics · All topics