Git practice questions with answers
Practise Git questions at your own pace: branches, merge vs rebase, reset, revert, stash and recovering lost work. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.
Which command creates a new branch and switches to it?
easy
- git branch -d feature
- git switch -c feature
- git merge feature
- git push feature
Answer: B. git switch -c feature
Explanation: git switch -c feature (or the older git checkout -b feature).
What does git status show?
easy
- The commit history
- Changed, staged and untracked files in the working tree
- Remote branches only
- The diff of the last commit
Answer: B. Changed, staged and untracked files in the working tree
Explanation: Use git log for history and git diff for line-by-line changes.
What does git pull do?
easy
- Only downloads changes
- Fetches changes from the remote and merges (or rebases) them into the current branch
- Uploads your commits
- Deletes local changes
Answer: B. Fetches changes from the remote and merges (or rebases) them into the current branch
Explanation: git pull = git fetch + git merge by default. git fetch alone downloads without touching your branch.
Which file lists paths Git should not track?
easy
- .gitconfig
- .gitignore
- .gitkeep
- README.md
Answer: B. .gitignore
Explanation: Typical entries are node_modules/, .env and build outputs. Never commit secrets: ignore .env files.
What is the difference between git merge and git rebase?
medium
- They are identical
- Merge joins histories with a merge commit; rebase replays your commits on top of another branch for a linear history
- Rebase deletes branches
- Merge only works on remote branches
Answer: B. Merge joins histories with a merge commit; rebase replays your commits on top of another branch for a linear history
Explanation: Do not rebase commits that others have already pulled: it rewrites their hashes.
You committed to the wrong message. The commit is not pushed yet. Which command fixes the message?
medium
- git revert HEAD
- git commit --amend
- git reset --hard
- git stash
Answer: B. git commit --amend
Explanation: --amend replaces the last commit (message and staged changes). Avoid amending commits already pushed to a shared branch.
What does git stash do?
medium
- Deletes uncommitted changes
- Saves uncommitted changes aside and cleans the working tree, to be restored later with git stash pop
- Pushes to a hidden branch on the remote
- Squashes commits
Answer: B. Saves uncommitted changes aside and cleans the working tree, to be restored later with git stash pop
Explanation: Handy for switching branches quickly without committing half-done work.
How do you undo a commit that is already pushed to a shared branch?
medium
- git reset --hard HEAD~1 and force-push
- git revert <commit>, which adds a new commit that undoes it
- Delete the repository
- git checkout <commit>
Answer: B. git revert <commit>, which adds a new commit that undoes it
Explanation: Revert keeps history intact for everyone. Resetting and force-pushing rewrites history that teammates already have.
What does git reset --soft HEAD~1 do?
hard
- Deletes the last commit and its changes
- Removes the last commit but keeps its changes staged
- Reverts the last commit with a new commit
- Resets the remote
Answer: B. Removes the last commit but keeps its changes staged
Explanation: --soft keeps changes staged, --mixed (the default) keeps them unstaged, --hard throws them away.
What is a detached HEAD?
hard
- A corrupted repository
- HEAD points directly at a commit instead of a branch; new commits are not on any branch unless you create one
- A deleted branch
- A merge conflict
Answer: B. HEAD points directly at a commit instead of a branch; new commits are not on any branch unless you create one
Explanation: It happens after git checkout <commit-hash>. Run git switch -c new-branch to keep work made there.
Which command finds the commit that introduced a bug by binary search?
hard
- git blame
- git bisect
- git log --grep
- git reflog
Answer: B. git bisect
Explanation: Mark a good and a bad commit and Git checks out the midpoints for you to test: about log₂(n) steps.
You ran git reset --hard and lost a commit. What can recover it? (Select all that apply)
hard
- git reflog to find the commit hash, then git switch -c rescue <hash>
- git cherry-pick <hash> once you know the hash
- git clean -fd
- Nothing, it is gone forever
Answer: A. git reflog to find the commit hash, then git switch -c rescue <hash>; B. git cherry-pick <hash> once you know the hash
Explanation: Commits stay in the object store for a while after reset. The reflog records where HEAD has been. git clean deletes untracked files and does not help.