• Codetuts
  • Posts
  • Git Quiz – Hard Level (25 Questions)

Git Quiz – Hard Level (25 Questions)

Question 1:

You are working on a branch feature-x. After merging it into main, you realize that some files were accidentally deleted during the merge. What is the best way to recover those files without affecting other changes?

  1. git checkout main and manually add the files back

  2. Use git cherry-pick to recover specific commits

  3. Use git revert on the merge commit

  4. Use git checkout to recover the deleted files from the previous commit

Question 2:

Your colleague pushed a broken commit to the shared main branch. How can you undo this commit and remove it from the branch's history?

  1. Use git reset --soft and force-push

  2. Use git revert to create a new commit reversing changes

  3. Use git reset --hard to erase the commit locally

  4. Use git cherry-pick to revert the commit

Question 3:

While resolving a merge conflict, you accidentally stage a file with incorrect resolution. What should you do to re-edit the conflict?

  1. Use git reset HEAD to unstage and edit the file

  2. Use git merge --abort and restart the process

  3. Use git checkout --conflict to reset the conflicted file

  4. Use git rebase to rewind the commit

Question 4:

You cloned a large repository but don’t need all the commit history. How can you clone only the latest 5 commits to save bandwidth?

  1. Use git clone --depth 5

  2. Use git fetch --prune

  3. Use git clone --shallow-since

  4. Use git archive

Question 5:

A branch named bugfix was deleted by mistake. How can you recover the branch along with its commits?

  1. git stash pop

  2. git reflog and checkout the branch’s hash

  3. git pull origin bugfix

  4. Use git rebase

Question 6:

Your Git repository’s history is cluttered with multiple unnecessary commits. How can you combine the last 10 commits into a single commit?

  1. Use git rebase -i HEAD~10

  2. Use git squash --all

  3. Use git commit --amend repeatedly

  4. Use git merge --squash HEAD~10

Question 7:

You want to find out which commit introduced a bug in your code. What Git feature is best suited for this purpose?

  1. git log --grep

  2. git bisect

  3. git blame

  4. git diff

Question 8:

Your feature-x branch has diverged from the main branch after several commits. To cleanly integrate changes from main into feature-x, what is the best approach?

  1. git rebase main onto feature-x

  2. git cherry-pick main commits

  3. git merge main into feature-x

  4. Delete and re-create the feature-x branch

Question 9:

You need to revert a commit made 3 commits ago without affecting later commits. What is the most efficient way to do this?

  1. git reset --hard HEAD~3

  2. git revert HEAD~2

  3. git checkout HEAD~3

  4. git stash

Question 10:

You want to see the list of files that were changed between two specific commits. Which Git command will help?

  1. git diff

  2. git log --stat

  3. git diff <commit1> <commit2>

  4. git show <commit>

Question 11:

You accidentally committed sensitive data in a file. How can you remove it from both the commit history and remote repository?

  1. git reset HEAD and commit again

  2. git rm --cached and push the changes

  3. Use git filter-repo or git filter-branch

  4. git stash and delete the file

Question 12:

What is the best way to avoid merge conflicts when working on a shared feature branch?

  1. Always rebase your branch before pushing

  2. Use git cherry-pick for merging changes

  3. Always use git pull --merge instead of rebase

  4. Push frequently and coordinate with team members

Question 13:

You accidentally made changes in the main branch instead of feature-x. How can you move the changes to the correct branch?

  1. Use git stash and apply it to the new branch

  2. Use git cherry-pick to move the commit

  3. Use git merge to integrate changes

  4. Use git reset and recommit on the new branch

Question 14:

How do you prevent a specific file from being pushed to a Git repository, even if it is already tracked?

  1. Add the file to .gitignore

  2. Use git rm --cached and commit

  3. Use git stash before pushing

  4. Use git reset on the file

Question 15:

Your repository has a submodule. How do you update it to the latest version of the referenced commit?

  1. git submodule update --remote

  2. git fetch inside the submodule folder

  3. git pull origin from the submodule folder

  4. git rebase

Question 16:

You are rebasing a branch and encounter merge conflicts. What is the correct order of steps to resolve this?

  1. Resolve conflicts → git addgit rebase --continue

  2. Abort rebase → Resolve conflicts → Retry

  3. Resolve conflicts → git commitgit rebase --abort

  4. Use git stash and retry rebase

Question 17:

How can you enforce a pre-commit hook for all developers on a team?

  1. Share the .git/hooks/pre-commit script via email

  2. Include the hook in the repository and use git config

  3. Use a third-party tool like Husky or Lefthook

  4. Use git stash for changes before committing

Question 18:

How do you squash commits during a rebase?

  1. Use git rebase --squash

  2. Use git rebase -i and mark commits as "squash"

  3. Use git merge --squash

  4. Use git cherry-pick --squash

Question 19:

You want to compare the current state of your branch with another branch. Which command will help?

  1. git log branch1..branch2

  2. git diff branch1 branch2

  3. git status branch2

  4. git compare branch1 branch2

Question 20:

You need to create a patch file containing changes in a branch. How do you do this?

  1. git diff > changes.patch

  2. git format-patch

  3. git log > patch.patch

  4. git archive

Question 21:

How do you change the author of the last commit?

  1. git commit --author

  2. git rebase -i and edit the commit

  3. git commit --amend --author

  4. git filter-repo

Question 22:

What’s the safest way to handle a Git conflict in a binary file?

  1. Keep the older version of the file

  2. Use git reset and start fresh

  3. Manually overwrite the file and commit

  4. Replace the file with a clean copy

Question 23:

How do you track down a commit that introduced a memory leak across multiple branches?

  1. git bisect

  2. git blame

  3. git show

  4. git cherry

Question 24:

What is the easiest way to temporarily store your uncommitted changes to switch branches?

  1. git reset

  2. git stash

  3. git cherry-pick

  4. git checkout --force

Question 25:

How do you ensure that only signed commits are pushed to a remote repository?

  1. Enforce signed commits using .gitconfig

  2. Use a pre-push Git hook

  3. Enable GPG commit verification in the repository settings

  4. Require GPG verification in the remote branch rules

 Git Quiz – Hard Level (25 Answers)

Question 1:

Answer: 4. Use git checkout to recover the deleted files from the previous commit
Explanation:
You can recover deleted files by using git checkout or git restore to revert the file to its state in the previous commit. For example:

git checkout HEAD^ -- path/to/file

Question 2:

Answer: 1. Use git reset --soft and force-push
Explanation:
To remove a commit from history, use git reset to move back in the commit tree, and then force-push to overwrite the remote branch. For example:

git reset --soft HEAD~1

git push origin main --force

Question 3:

Answer: 1. Use git reset HEAD to unstage and edit the file
Explanation:
If you have staged an incorrect conflict resolution, you can unstage the file using git reset HEAD and re-edit it.

Question 4:

Answer: 1. Use git clone --depth 5
 Explanation:
Shallow cloning with --depth fetches only the specified number of commits, saving bandwidth. Example:

git clone --depth 5 <repo-url>

Question 5:

Answer: 2. git reflog and checkout the branch’s hash
Explanation:
Git’s reflog tracks references. You can use git reflog to find the hash of the deleted branch and recreate it:

git checkout -b bugfix <hash>

Question 6:

Answer: 1. Use git rebase -i HEAD~10
 Explanation:
Interactive rebasing allows you to combine multiple commits. Mark commits as squash in the interactive editor.

Question 7:

Answer: 2. git bisect
 Explanation:
 git bisect helps find the commit that introduced a bug by performing a binary search through commit history.

Question 8:

Answer: 1. git rebase main onto feature-x
 Explanation:
Rebasing integrates changes from main into feature-x by applying feature-x commits on top of the updated main branch.

Question 9:

Answer: 2. git revert HEAD~2
 Explanation:
 git revert creates a new commit that undoes changes from a previous commit without affecting the history.

Question 10:

Answer: 3. git diff <commit1> <commit2>
 Explanation:
This command shows differences between the specified commits, including the list of changed files.

Question 11:

Answer: 3. Use git filter-repo or git filter-branch
 Explanation:
Both commands can rewrite commit history to remove sensitive data. git filter-repo is the newer, faster alternative.

Question 12:

Answer: 4. Push frequently and coordinate with team members
Explanation:
Frequent pushes and team coordination reduce the likelihood of overlapping changes, preventing conflicts.

Question 13:

Answer: 1. Use git stash and apply it to the new branch
Explanation:
Stashing temporarily saves changes. You can then switch branches and apply the stash:

git stash

git checkout feature-x

git stash pop

Question 14:

Answer: 2. Use git rm --cached and commit
Explanation:
The .gitignore file does not affect already tracked files. Use git rm --cached to stop tracking the file.

Question 15:

Answer: 1. git submodule update --remote
 Explanation:
To update a submodule, use this command to fetch and update to the latest referenced commit.

Question 16:

Answer: 1. Resolve conflicts → git addgit rebase --continue
 Explanation:
During a rebase, resolve conflicts manually, stage the files, and continue the rebase:

git add <file>

git rebase --continue

Question 17:

Answer: 3. Use a third-party tool like Husky or Lefthook
Explanation:
Tools like Husky allow you to enforce hooks across all team members by configuring them in the repository.

Question 18:

Answer: 2. Use git rebase -i and mark commits as "squash"
Explanation:
During an interactive rebase, you can squash commits by marking them as squash in the editor.

Question 19:

Answer: 2. git diff branch1 branch2
 Explanation:
This command compares the differences between two branches.

Question 20:

Answer: 2. git format-patch
 Explanation:
 git format-patch creates a patch file for sharing commits or changes.

Question 21:

Answer: 3. git commit --amend --author
 Explanation:
This command allows you to modify the author of the most recent commit. Example:

git commit --amend --author="Name <email>"

Question 22:

Answer: 3. Manually overwrite the file and commit
Explanation:
Git cannot merge binary files automatically. You must manually resolve and overwrite the file.

Question 23:

Answer: 1. git bisect
 Explanation:
This tool performs a binary search to find the commit that introduced an issue, even across branches.

Question 24:

Answer: 2. git stash
 Explanation:
Stashing temporarily saves your uncommitted changes so you can switch branches. Example:

git stash

git checkout branch-name

Question 25:

Answer: 3. Enable GPG commit verification in the repository settings
Explanation:
Repositories can enforce GPG commit verification to ensure only signed commits are   \pushed.

Reply

or to participate.