Git Git Branching and Merging 1 — Questions and Answers
Question 1: Which command creates a new branch and switches to it in one step?
- git branch new-branch
- git checkout -b new-branch (Correct answer)
- git switch new-branch
- git create new-branch
Correct answer: git checkout -b new-branch
The `git checkout -b new-branch` command creates a new branch and immediately switches to it.
Question 2: What does a fast-forward merge do?
- Creates a new merge commit
- Moves the base branch pointer forward to the tip of the feature branch (Correct answer)
- Deletes the feature branch automatically
- Squashes all commits into one
Correct answer: Moves the base branch pointer forward to the tip of the feature branch
A fast-forward merge simply advances the pointer of the base branch to the latest commit of the feature branch when there is a linear history.
Question 3: Which command lists all local and remote branches?
- git branch
- git branch -r
- git branch -a (Correct answer)
- git branch --list
Correct answer: git branch -a
The `git branch -a` command displays all local and remote-tracking branches.
Question 4: What is a merge conflict?
- When a branch is deleted during a merge
- When two branches have changes to the same lines of a file that Git cannot automatically reconcile (Correct answer)
- When the network fails during a push
- When a commit message is missing
Correct answer: When two branches have changes to the same lines of a file that Git cannot automatically reconcile
A merge conflict occurs when two branches modify the same lines of a file and Git cannot determine which change to keep.
Question 5: Which `git merge` option always creates a merge commit even if fast-forward is possible?
- --squash
- --rebase
- --no-ff (Correct answer)
- --force
Correct answer: --no-ff
The `--no-ff` flag forces Git to create a merge commit regardless of whether a fast-forward is possible.
Question 6: How do you delete a local branch in Git?
- git branch -d branch-name (Correct answer)
- git delete branch-name
- git remove branch-name
- git branch --remove branch-name
Correct answer: git branch -d branch-name
The `git branch -d branch-name` command safely deletes a local branch that has been fully merged.
Which command creates a new branch and switches to it in one step?