Hack4Hack

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.

  1. Which command creates a new branch and switches to it?

    easy

    1. git branch -d feature
    2. git switch -c feature
    3. git merge feature
    4. git push feature

    Answer: B. git switch -c feature

    Explanation: git switch -c feature (or the older git checkout -b feature).

  2. What does git status show?

    easy

    1. The commit history
    2. Changed, staged and untracked files in the working tree
    3. Remote branches only
    4. 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.

  3. What does git pull do?

    easy

    1. Only downloads changes
    2. Fetches changes from the remote and merges (or rebases) them into the current branch
    3. Uploads your commits
    4. 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.

  4. Which file lists paths Git should not track?

    easy

    1. .gitconfig
    2. .gitignore
    3. .gitkeep
    4. README.md

    Answer: B. .gitignore

    Explanation: Typical entries are node_modules/, .env and build outputs. Never commit secrets: ignore .env files.

  5. What is the difference between git merge and git rebase?

    medium

    1. They are identical
    2. Merge joins histories with a merge commit; rebase replays your commits on top of another branch for a linear history
    3. Rebase deletes branches
    4. 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.

  6. You committed to the wrong message. The commit is not pushed yet. Which command fixes the message?

    medium

    1. git revert HEAD
    2. git commit --amend
    3. git reset --hard
    4. 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.

  7. What does git stash do?

    medium

    1. Deletes uncommitted changes
    2. Saves uncommitted changes aside and cleans the working tree, to be restored later with git stash pop
    3. Pushes to a hidden branch on the remote
    4. 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.

  8. How do you undo a commit that is already pushed to a shared branch?

    medium

    1. git reset --hard HEAD~1 and force-push
    2. git revert <commit>, which adds a new commit that undoes it
    3. Delete the repository
    4. 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.

  9. What does git reset --soft HEAD~1 do?

    hard

    1. Deletes the last commit and its changes
    2. Removes the last commit but keeps its changes staged
    3. Reverts the last commit with a new commit
    4. 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.

  10. What is a detached HEAD?

    hard

    1. A corrupted repository
    2. HEAD points directly at a commit instead of a branch; new commits are not on any branch unless you create one
    3. A deleted branch
    4. 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.

  11. Which command finds the commit that introduced a bug by binary search?

    hard

    1. git blame
    2. git bisect
    3. git log --grep
    4. 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.

  12. You ran git reset --hard and lost a commit. What can recover it? (Select all that apply)

    hard

    1. git reflog to find the commit hash, then git switch -c rescue <hash>
    2. git cherry-pick <hash> once you know the hash
    3. git clean -fd
    4. 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.

Play the Daily Quiz at 4 PM IST