Git Junior cheat sheet: interview answers

Every question of this level with its correct answer and explanation, on one page for a quick review before the interview.

  1. 1. What makes Git a distributed version control system?

    Correct answer: Every clone has the full history and can work offline

    A clone contains every commit, so you can commit, branch and view history without a server. A remote like GitHub is just another copy that the team agrees to sync with.

  2. 2. What are the three main areas a file moves through in Git?

    Correct answer: Working tree, staging area (index), repository

    You edit files in the working tree, choose what goes into the next commit with git add (the index), and git commit stores that snapshot in the repository.

  3. 3. What does git add file.js do?

    Correct answer: Stages the file's current content for the next commit

    git add copies the file's current state into the index. If you edit the file again after that, the new changes aren't staged until you run git add again.

  4. 4. What does a Git commit store?

    Correct answer: A snapshot of the project plus author, message and parent

    Conceptually each commit is a full snapshot (unchanged files are reused, not duplicated) with metadata and a link to its parent. Diffs are computed when you ask for them.

  5. 5. What identifies a commit in Git?

    Correct answer: A hash of its content and metadata

    A commit's ID is a SHA-1 (or SHA-256) hash of its tree, parents, author and message. Change anything and the hash changes, which is why rewriting history creates new commits.

  6. 6. Which command shows modified, staged and untracked files?

    Correct answer: git status

    git status compares the working tree, the index and the last commit. git log lists commits, and git show displays one commit.

  7. 7. What does plain git diff show?

    Correct answer: Changes in the working tree that are not staged yet

    git diff compares the working tree with the index. Use git diff --staged to see what will go into the next commit, and git diff HEAD for both.

  8. 8. How do you see what is staged for the next commit?

    Correct answer: git diff --staged

    --staged (or its synonym --cached) compares the index with the last commit. It's worth checking before committing to make sure nothing extra is included.

  9. 9. You added .env to .gitignore, but Git still tracks changes to it. Why?

    Correct answer: The file was already committed; ignore rules apply only to untracked files

    Run git rm --cached .env to stop tracking it while keeping the file on disk, then commit. If it contained secrets, they are still in history, so rotate them.

  10. 10. What does git rm --cached file do?

    Correct answer: Stops tracking the file but keeps it on disk

    The file is removed from the index, so the next commit records its deletion from the repository, while the working copy stays. Without --cached it would be deleted from disk too.

  11. 11. Which .gitignore line ignores every node_modules folder in the project?

    Correct answer: node_modules/

    A pattern without a leading slash matches at any depth, and the trailing / limits it to directories. A leading / would match only at the repository root.

  12. 12. Why doesn't Git track an empty directory?

    Correct answer: Git tracks files, and a directory exists only through its files

    A tree object lists files and subtrees, so an empty folder has nothing to record. The usual workaround is a placeholder file such as .gitkeep.

  13. 13. What does git clone <url> do?

    Correct answer: Copies the whole repo and checks out the default branch

    Clone downloads all commits, sets up a remote called origin and checks out the default branch. Shallow clones (--depth) are an option when history isn't needed.

  14. 14. What is origin?

    Correct answer: The default name of the remote you cloned from

    origin is just a conventional name for a remote URL. You can rename it or add others, for example upstream for the original repository of a fork.

  15. 15. What is the difference between git fetch and git pull?

    Correct answer: fetch only downloads; pull downloads and integrates into your branch

    git pull is git fetch followed by a merge (or a rebase, if configured). Fetching first lets you inspect incoming commits before changing your branch.

  16. 16. git push is rejected with "non-fast-forward". What does it mean?

    Correct answer: The remote has commits you don't have; integrate them first

    Someone pushed new commits, and your branch doesn't include them. Pull (merge or rebase), resolve any conflicts, then push. Forcing would overwrite their work.

  17. 17. What is a branch in Git?

    Correct answer: A movable pointer to a commit

    A branch is a tiny file containing a commit hash; when you commit on it, the pointer moves forward. That's why creating and switching branches is instant.

  18. 18. What is HEAD?

    Correct answer: A pointer to what is checked out, usually a branch

    HEAD normally points to a branch, which points to a commit. New commits go to the branch HEAD refers to.

  19. 19. How do you create a branch and switch to it in one command?

    Correct answer: git switch -c feature

    git switch -c (or the older git checkout -b) creates the branch at the current commit and switches to it. git branch feature alone only creates it.

  20. 20. What is a fast-forward merge?

    Correct answer: Moving the branch pointer ahead when there are no divergent commits

    If main hasn't moved since feature branched off, merging just moves main to feature's commit, with no merge commit. --no-ff forces a merge commit anyway.

  21. 21. When does Git create a merge commit?

    Correct answer: When both branches have new commits since they diverged

    With divergent histories Git combines them in a commit with two parents. Conflicts or not, that commit is needed; with a linear history a fast-forward is enough.

  22. 22. What do the markers <<<<<<<, ======= and >>>>>>> in a file mean?

    Correct answer: An unresolved merge conflict: your version and theirs

    Edit the file to the desired result, remove the markers, git add it and continue the merge (git commit or git merge --continue).

  23. 23. How do you abort a merge that has conflicts and go back to the previous state?

    Correct answer: git merge --abort

    --abort restores the branch and working tree to how they were before the merge started. For a rebase the equivalent is git rebase --abort.

  24. 24. How do you delete a local branch that has been merged?

    Correct answer: git branch -d feature

    -d refuses to delete a branch with unmerged commits; -D forces it. Deleting a branch removes only the pointer; commits reachable from other branches stay.

  25. 25. How do you delete a branch on the remote?

    Correct answer: git push origin --delete feature

    git branch -d origin/feature would only delete your local remote-tracking copy. Hosting platforms can also delete branches automatically after a pull request is merged.

  26. 26. What is origin/main?

    Correct answer: Your local copy of where main was on the remote at the last fetch

    Remote-tracking branches update only on fetch, pull or push. That's why git status can say "up to date" while the server already has newer commits.

  27. 27. How do you fix the message of the last commit before pushing?

    Correct answer: git commit --amend

    --amend replaces the last commit with a new one, so you can also add forgotten files. Since it rewrites history, avoid it on commits that others already pulled.

  28. 28. How do you discard uncommitted changes to one file?

    Correct answer: git restore file.js

    git restore (or the older git checkout -- file) replaces the working copy with the version from the index. The discarded changes can't be recovered by Git.

  29. 29. How do you unstage a file but keep its changes?

    Correct answer: git restore --staged file.js

    --staged resets the index entry to the last commit, while the working copy stays as is. git reset file.js does the same.

  30. 30. What does git revert <commit> do?

    Correct answer: Creates a new commit that undoes that commit's changes

    History stays intact and the fix is just another commit, so revert is safe on shared branches such as main. Reset, by contrast, moves the branch and rewrites history.

  31. 31. When is git reset risky and git revert preferable?

    Correct answer: When the commits were already pulled by others

    Reset rewrites history; teammates who have the old commits will get confusing divergence. For published history add a reverting commit instead.

  32. 32. What does git stash do?

    Correct answer: Saves uncommitted changes aside and cleans the working tree

    Use it to switch tasks quickly; git stash pop reapplies the changes. By default untracked files aren't stashed; add -u for them.

  33. 33. What is the difference between git stash pop and git stash apply?

    Correct answer: pop applies and removes the stash; apply keeps it

    If applying causes conflicts, pop keeps the stash anyway so nothing is lost. apply is handy when you want the same changes on several branches.

  34. 34. What is a tag in Git usually used for?

    Correct answer: Marking a commit, such as a release v1.2.0

    A tag is a fixed name for a commit that doesn't move like a branch. Annotated tags (git tag -a) also store a message, author and date.

  35. 35. Are tags pushed by a plain git push?

    Correct answer: No, push them with git push origin v1.0 or --tags

    Tags must be pushed explicitly. git push --follow-tags pushes annotated tags that point to the commits being pushed.

  36. 36. Which command shows a compact one-line history?

    Correct answer: git log --oneline

    --oneline prints a short hash and the subject. Add --graph --all to see how branches diverge and merge.

  37. 37. How do you find who last changed each line of a file?

    Correct answer: git blame file.js

    git blame annotates each line with the commit and author that last touched it. It's for understanding context, not for blaming people.

  38. 38. What does git commit -a -m "msg" do?

    Correct answer: Stages changes to tracked files and commits them

    -a stages modifications and deletions of already tracked files. New, untracked files still need an explicit git add.

  39. 39. Why should you set user.name and user.email in Git?

    Correct answer: They are written into every commit as the author

    Authentication to a server uses SSH keys or tokens; name and email only label commits. Platforms link commits to accounts by email.

  40. 40. What does git init do?

    Correct answer: Creates a new repository in the current folder

    It creates the .git directory with an empty history. Files in the folder stay untracked until you add and commit them.

  41. 41. What is in the .git folder?

    Correct answer: The whole repository: objects, refs and configuration

    Deleting .git deletes all history and branches, leaving just the current files. Never edit it by hand unless you know what you're doing.

  42. 42. What is a "detached HEAD"?

    Correct answer: HEAD points to a commit directly, not to a branch

    It happens after checking out a commit or tag. You can look around and commit, but new commits belong to no branch; create one with git switch -c to keep them.

  43. 43. What is a pull request (merge request)?

    Correct answer: A proposal to merge a branch, with review

    Pull requests are a feature of GitHub, GitLab and similar platforms, not of Git itself. They host code review, CI results and the merge button.

  44. 44. What is a fork?

    Correct answer: Your server-side copy of another repository

    You push to your fork and open a pull request to the original repository. It's the usual way to contribute to open source without write access.

  45. 45. What makes a good commit message?

    Correct answer: A short imperative summary, then the why

    For example, "Fix crash when cart is empty", then a blank line and an explanation. The diff already shows what changed; the message should say why.

  46. 46. What is the main idea of small, focused commits?

    Correct answer: Each commit does one thing, easy to review and revert

    Mixing a refactor, a bug fix and formatting in one commit makes review hard and turns a targeted revert into a big mess.

  47. 47. You accidentally committed an API key and pushed it. What must you do first?

    Correct answer: Revoke and rotate the key, then clean it out of history

    Once pushed, assume it's compromised: bots scan public repositories within minutes. A new commit doesn't remove the key from history, and rewriting history doesn't help clones others already have.

  48. 48. What does git log main..feature show?

    Correct answer: Commits in feature that are not in main

    The two-dot range means "reachable from the right side but not from the left". It's a quick way to see what a branch would bring in a merge.

  49. 49. Which command shows the changes introduced by a single commit?

    Correct answer: git show <commit>

    git show prints the commit's metadata and its diff against the parent. It also works with tags and files at a given commit, such as git show HEAD:package.json.

  50. 50. What does HEAD~1 refer to?

    Correct answer: The parent of the current commit

    ~n goes back n generations along first parents: HEAD~2 is the grandparent. HEAD^2 means the second parent of a merge commit.

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

    Correct answer: Undoes the last commit but keeps its changes staged

    The branch moves back one commit while the index and files stay as they were. It's a quick way to redo a commit, for example to split it.

  52. 52. What is the danger of git reset --hard?

    Correct answer: Uncommitted changes in the working tree are lost

    --hard overwrites the index and working tree. Committed work can usually be recovered through the reflog, but uncommitted edits are gone.

  53. 53. Why is node_modules usually not committed?

    Correct answer: It's huge and can be rebuilt from the lockfile

    Commit package.json and the lockfile instead: npm ci installs exactly the same versions on any machine, and the repository stays small.

  54. 54. What is git switch for compared with git checkout?

    Correct answer: It only switches branches, a safer subset of checkout

    checkout did two unrelated jobs: switching branches and restoring files. Git 2.23 split them into git switch and git restore to reduce mistakes.

  55. 55. What does git branch -a list?

    Correct answer: Local branches and remote-tracking branches

    Remote-tracking branches appear as remotes/origin/.... Run git fetch --prune to drop ones that were deleted on the server.

  56. 56. How do you set the upstream branch when pushing a new branch for the first time?

    Correct answer: git push -u origin feature

    -u (--set-upstream) links the local branch to origin/feature, so later a plain git push or git pull knows where to go.

  57. 57. What does git clean -n do?

    Correct answer: Shows which untracked files would be deleted

    -n is a dry run; -f actually deletes. Untracked files aren't in Git, so once cleaned they can't be recovered from it.

  58. 58. Why can git pull create unexpected merge commits like "Merge branch 'main' of ..."?

    Correct answer: By default pull merges when local and remote have both moved

    If you committed locally and someone pushed meanwhile, the default pull merges them. git pull --rebase (or pull.rebase = true) replays your commits on top instead.

  59. 59. What is a .gitattributes file used for?

    Correct answer: Per-path settings like line endings or LFS tracking

    For example * text=auto normalizes line endings, and *.png filter=lfs stores images in Git LFS. It's committed, so the whole team gets the same rules.

  60. 60. Why do Windows and macOS users sometimes see every line of a file as changed?

    Correct answer: Different line endings (CRLF vs LF) were committed

    Configure core.autocrlf or, better, a .gitattributes rule like * text=auto eol=lf, so the repository stores LF and each OS gets what it needs.

  61. 61. Which file is a bad idea to commit to a Git repository?

    Correct answer: A .env file with production passwords

    Secrets in Git spread to every clone and stay in history. Commit a .env.example with placeholder values and keep real secrets in a vault or the CI settings.

  62. 62. What does git mv old.js new.js do?

    Correct answer: Renames the file and stages the rename

    Git doesn't store renames explicitly; it detects them by comparing content. git mv is a shortcut for renaming plus git rm and git add.

  63. 63. What does git log -p file.js show?

    Correct answer: The file's history with each commit's diff

    -p adds patches to the log. Add --follow to keep tracking the file across renames.

  64. 64. What does "Your branch is ahead of 'origin/main' by 2 commits" mean?

    Correct answer: You have 2 local commits that aren't pushed yet

    Compared with your last fetch, your branch has 2 extra commits. "Behind" means the opposite; "diverged" means both have commits the other lacks.

  65. 65. Why should you pull or fetch before starting work on a shared branch?

    Correct answer: To build on the latest commits and avoid needless conflicts

    Git doesn't lock anything; working from an old base just means more integration work later. Short-lived branches from a fresh main keep conflicts small.

  66. 66. What does git diff main feature compare?

    Correct answer: The snapshots at the tips of the two branches

    It shows how to turn one snapshot into the other. git diff main...feature (three dots) shows only what feature changed since it branched off, which is what a pull request displays.

  67. 67. What is a lockfile conflict in package-lock.json usually best resolved with?

    Correct answer: Take one side, then run npm install to regenerate it

    Lockfiles are generated, so hand-merging is error-prone. Keep package.json correct and let npm rebuild a consistent lockfile, then review the result.

  68. 68. What does git checkout v1.0 (a tag) leave you in?

    Correct answer: A detached HEAD at the tagged commit

    Tags don't move, so you can't commit "on" them. To fix an old release, create a branch from the tag: git switch -c hotfix v1.0.

  69. 69. What is semantic versioning (MAJOR.MINOR.PATCH)?

    Correct answer: Bump MAJOR for breaking changes, MINOR for features, PATCH for fixes

    SemVer tells users what to expect from an upgrade. Release tags like v2.4.1 usually follow it, and tools can bump versions from commit messages.

  70. 70. Which statement about deleting a branch is true?

    Correct answer: Commits stay in history if another branch or tag reaches them

    A branch is only a pointer. Merged commits are part of main's history; unmerged ones become unreachable and are eventually garbage-collected, but the reflog keeps them for a while.

  71. 71. What does git commit --amend --no-edit do?

    Correct answer: Adds staged changes to the last commit, same message

    Handy when you forgot a file: stage it and amend without retyping the message. The commit still gets a new hash.

  72. 72. What is the purpose of a README.md in the repository root?

    Correct answer: It's shown on the repo page to explain the project

    Platforms render it on the main page. A good README says what the project is, how to run it and how to contribute.

  73. 73. What does git log --author="Ann" do?

    Correct answer: Lists only commits whose author matches "Ann"

    --author filters by a pattern in the author field. Combine it with --since="2 weeks ago" or a path to narrow the history further.

  74. 74. What is the difference between tracked and untracked files?

    Correct answer: Tracked files were in the last commit or are staged

    New files are untracked until you git add them; Git doesn't include them in commits. Ignored files are a separate category: untracked files that git status doesn't even show.

  75. 75. Two people edited different functions in the same file. What happens when their branches merge?

    Correct answer: Git usually merges them automatically without a conflict

    Merging works on changed regions of lines, not whole files. Conflicts appear only when both sides changed the same or adjacent lines.