Git Basics Questions with Answers

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

Sample questions

Question 1Easy

You ran git add notes.txt by mistake. Which command unstages it but keeps your edits?

  1. git rm notes.txt
  2. git restore --staged notes.txt
  3. git restore notes.txt
  4. git clean notes.txt

Answer: B. git restore --staged notes.txt

Explanation

git restore --staged resets the index entry to HEAD, leaving the working file as it is. git reset notes.txt does the same.

Step-by-step solution

  1. File is staged.
  2. git restore --staged copies the HEAD entry into the index.
  3. The working file keeps your edits.

Why the other options are wrong

  • A. git rm would delete the file.
  • C. Without --staged, it would discard your edits.
  • D. git clean only deletes untracked files.

Question 2Easy

What does git clean -n do?

  1. Deletes every untracked file immediately without asking for confirmation
  2. Removes ignored files only
  3. Resets tracked files to HEAD
  4. Shows which untracked files would be deleted, without deleting

Answer: D. Shows which untracked files would be deleted, without deleting

Explanation

-n is a dry run. git clean -f actually deletes untracked files.

Step-by-step solution

  1. git clean targets untracked files.
  2. -n only previews.
  3. Use -f to delete for real.

Why the other options are wrong

  • A. Deleting needs -f.
  • B. Ignored files need -x.
  • C. Clean never touches tracked files.

Tip: Always dry-run first: it is irreversible.

Question 3Easy

Which command sets the name Git writes in new commits for all your repositories?

  1. git init --name "Your Name"
  2. git config --global user.name "Your Name"
  3. git commit --author-global
  4. git remote add name

Answer: B. git config --global user.name "Your Name"

Explanation

--global writes to ~/.gitconfig; without it, the setting applies to the current repository only.

Step-by-step solution

  1. Run git config --global user.name ....
  2. It is stored in ~/.gitconfig.
  3. New commits use that name.

Why the other options are wrong

  • A. git init has no such option.
  • C. No such commit option exists.
  • D. remote add configures remotes.

Question 4Easy

What does git diff --staged (or --cached) compare?

  1. The working tree against the staging area (what plain git diff shows)
  2. The staging area against the last commit (HEAD)
  3. The local branch against the remote branch
  4. Two branches by name

Answer: B. The staging area against the last commit (HEAD)

Explanation

--staged shows what would go into the next commit: index versus HEAD.

Step-by-step solution

  1. Plain git diff is working tree vs index.
  2. --staged shifts to index vs HEAD.
  3. Use it to review before committing.

Why the other options are wrong

  • A. That is plain git diff without options.
  • C. Comparing with a remote needs git diff origin/main.
  • D. Branch comparison is git diff A B.

Tip: Review with git diff --staged before every commit.

Question 5Medium

In a repository, what does HEAD~2 refer to?

  1. The commit two steps ahead of HEAD on the branch
  2. The second branch that was created in the repository
  3. The second parent of the current merge commit
  4. The grandparent of HEAD, following first parents

Answer: D. The grandparent of HEAD, following first parents

Explanation

~n walks n generations back along first parents (HEAD~2 is the grandparent). ^2 selects the second parent of a merge commit.

Step-by-step solution

  1. ~ walks back along first parents.
  2. HEAD~1 is the parent, HEAD~2 the parent's parent.
  3. So HEAD~2 is the grandparent.

Why the other options are wrong

  • A. ~ goes backwards in history, not ahead.
  • B. ~ counts commits, not branches.
  • C. The second parent of a merge is HEAD^2.

Tip: ~ = generations back; ^ = which parent.

Question 6Medium

What does git rm --cached secret.txt do?

  1. Deletes the file from the disk and also removes it from Git tracking entirely
  2. Deletes it from history of all commits
  3. Restores it from the last commit
  4. Removes it from the index so it is untracked, but keeps the file on disk

Answer: D. Removes it from the index so it is untracked, but keeps the file on disk

Explanation

--cached limits the removal to the staging area. The next commit stops tracking the file while your local copy stays.

Step-by-step solution

  1. The index entry is removed.
  2. The working file stays on disk.
  3. After commit the file is untracked (add it to .gitignore).

Why the other options are wrong

  • A. Without --cached, it would also delete the file.
  • B. Old commits still contain it; history is untouched.
  • C. Restoring is git restore.

Tip: Add the filename to .gitignore after this.

Question 7Medium

You commit and notice you forgot a file. The commit is not pushed. After git add forgot.txt, which command folds it into the last commit keeping the same message?

  1. git commit --amend --no-edit
  2. git commit --fixup
  3. git reset --hard HEAD
  4. git revert HEAD

Answer: A. git commit --amend --no-edit

Explanation

--amend replaces the last commit with a new one that includes the staged changes. --no-edit keeps the message.

Step-by-step solution

  1. Stage the forgotten file.
  2. git commit --amend --no-edit rewrites HEAD.
  3. The commit gets a new hash.

Why the other options are wrong

  • B. --fixup makes a separate commit meant for autosquash.
  • C. reset --hard discards the staged file.
  • D. revert adds an opposing commit.

Tip: Amend only unpushed commits.

Question 8Medium

You want to see what changed in the working tree compared with the last commit, staged and unstaged together. Which command?

  1. git diff --staged
  2. git diff
  3. git diff HEAD
  4. git log -p

Answer: C. git diff HEAD

Explanation

git diff HEAD compares the working tree to HEAD, merging staged and unstaged edits.

Step-by-step solution

  1. Plain git diff is working tree vs index.
  2. --staged is index vs HEAD.
  3. git diff HEAD covers both.

Why the other options are wrong

  • A. That shows only staged changes.
  • B. That shows only unstaged changes.
  • D. log -p shows past commits.

Question 9Hard

You run git commit --allow-empty -m "trigger CI". What happens?

  1. The command fails because nothing is staged
  2. A commit with no file changes is created
  3. The last commit is amended
  4. The remote is updated automatically

Answer: B. A commit with no file changes is created

Explanation

By default Git refuses commits with no changes; --allow-empty creates one anyway, often used to retrigger CI.

Step-by-step solution

  1. No changes are staged.
  2. --allow-empty overrides the check.
  3. A new commit with the same tree as the parent appears.

Why the other options are wrong

  • A. That is the default behaviour without the flag.
  • C. Amending needs --amend.
  • D. Pushing is separate.

Question 10Hard

After git reset --soft HEAD~1, what happens to the changes from the undone commit?

  1. They are lost
  2. They are moved to the stash
  3. They stay in the working tree and remain staged
  4. They stay in the working tree but are unstaged

Answer: C. They stay in the working tree and remain staged

Explanation

--soft moves only the branch pointer. The index and working tree are untouched, so the changes are still staged. --mixed (the default) would unstage them, and --hard would discard them.

Step-by-step solution

  1. --soft moves only the branch pointer back one commit.
  2. The staging area and working tree are untouched.
  3. So the changes stay in the files and remain staged.

Why the other options are wrong

  • A. Only --hard discards the changes.
  • B. reset never moves changes to the stash.
  • D. That is --mixed, the default.

Tip: soft keeps staged, mixed unstages, hard deletes.

Practise all 29 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