- 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?
git checkout main and manually add the files back
Use git cherry-pick to recover specific commits
Use git revert on the merge commit
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?
Use git reset --soft and force-push
Use git revert to create a new commit reversing changes
Use git reset --hard to erase the commit locally
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?
Use git reset HEAD to unstage and edit the file
Use git merge --abort and restart the process
Use git checkout --conflict to reset the conflicted file
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?
Use git clone --depth 5
Use git fetch --prune
Use git clone --shallow-since
Use git archive
Question 5:
A branch named bugfix was deleted by mistake. How can you recover the branch along with its commits?
git stash pop
git reflog and checkout the branch’s hash
git pull origin bugfix
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?
Use git rebase -i HEAD~10
Use git squash --all
Use git commit --amend repeatedly
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?
git log --grep
git bisect
git blame
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?
git rebase main onto feature-x
git cherry-pick main commits
git merge main into feature-x
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?
git reset --hard HEAD~3
git revert HEAD~2
git checkout HEAD~3
git stash
Question 10:
You want to see the list of files that were changed between two specific commits. Which Git command will help?
git diff
git log --stat
git diff <commit1> <commit2>
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?
git reset HEAD and commit again
git rm --cached and push the changes
Use git filter-repo or git filter-branch
git stash and delete the file
Question 12:
What is the best way to avoid merge conflicts when working on a shared feature branch?
Always rebase your branch before pushing
Use git cherry-pick for merging changes
Always use git pull --merge instead of rebase
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?
Use git stash and apply it to the new branch
Use git cherry-pick to move the commit
Use git merge to integrate changes
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?
Add the file to .gitignore
Use git rm --cached and commit
Use git stash before pushing
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?
git submodule update --remote
git fetch inside the submodule folder
git pull origin from the submodule folder
git rebase
Question 16:
You are rebasing a branch and encounter merge conflicts. What is the correct order of steps to resolve this?
Resolve conflicts → git add → git rebase --continue
Abort rebase → Resolve conflicts → Retry
Resolve conflicts → git commit → git rebase --abort
Use git stash and retry rebase
Question 17:
How can you enforce a pre-commit hook for all developers on a team?
Share the .git/hooks/pre-commit script via email
Include the hook in the repository and use git config
Use a third-party tool like Husky or Lefthook
Use git stash for changes before committing
Question 18:
How do you squash commits during a rebase?
Use git rebase --squash
Use git rebase -i and mark commits as "squash"
Use git merge --squash
Use git cherry-pick --squash
Question 19:
You want to compare the current state of your branch with another branch. Which command will help?
git log branch1..branch2
git diff branch1 branch2
git status branch2
git compare branch1 branch2
Question 20:
You need to create a patch file containing changes in a branch. How do you do this?
git diff > changes.patch
git format-patch
git log > patch.patch
git archive
Question 21:
How do you change the author of the last commit?
git commit --author
git rebase -i and edit the commit
git commit --amend --author
git filter-repo
Question 22:
What’s the safest way to handle a Git conflict in a binary file?
Keep the older version of the file
Use git reset and start fresh
Manually overwrite the file and commit
Replace the file with a clean copy
Question 23:
How do you track down a commit that introduced a memory leak across multiple branches?
git bisect
git blame
git show
git cherry
Question 24:
What is the easiest way to temporarily store your uncommitted changes to switch branches?
git reset
git stash
git cherry-pick
git checkout --force
Question 25:
How do you ensure that only signed commits are pushed to a remote repository?
Enforce signed commits using .gitconfig
Use a pre-push Git hook
Enable GPG commit verification in the repository settings
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 add → git 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